ide.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2024-2025 Intel Corporation. All rights reserved. */
  3. /* PCIe r7.0 section 6.33 Integrity & Data Encryption (IDE) */
  4. #define dev_fmt(fmt) "PCI/IDE: " fmt
  5. #include <linux/bitfield.h>
  6. #include <linux/bitops.h>
  7. #include <linux/pci.h>
  8. #include <linux/pci-ide.h>
  9. #include <linux/pci_regs.h>
  10. #include <linux/slab.h>
  11. #include <linux/sysfs.h>
  12. #include "pci.h"
  13. static int __sel_ide_offset(u16 ide_cap, u8 nr_link_ide, u8 stream_index,
  14. u8 nr_ide_mem)
  15. {
  16. u32 offset = ide_cap + PCI_IDE_LINK_STREAM_0 +
  17. nr_link_ide * PCI_IDE_LINK_BLOCK_SIZE;
  18. /*
  19. * Assume a constant number of address association resources per stream
  20. * index
  21. */
  22. return offset + stream_index * PCI_IDE_SEL_BLOCK_SIZE(nr_ide_mem);
  23. }
  24. static int sel_ide_offset(struct pci_dev *pdev,
  25. struct pci_ide_partner *settings)
  26. {
  27. return __sel_ide_offset(pdev->ide_cap, pdev->nr_link_ide,
  28. settings->stream_index, pdev->nr_ide_mem);
  29. }
  30. static bool reserve_stream_index(struct pci_dev *pdev, u8 idx)
  31. {
  32. int ret;
  33. ret = ida_alloc_range(&pdev->ide_stream_ida, idx, idx, GFP_KERNEL);
  34. return ret >= 0;
  35. }
  36. static bool reserve_stream_id(struct pci_host_bridge *hb, u8 id)
  37. {
  38. int ret;
  39. ret = ida_alloc_range(&hb->ide_stream_ids_ida, id, id, GFP_KERNEL);
  40. return ret >= 0;
  41. }
  42. static bool claim_stream(struct pci_host_bridge *hb, u8 stream_id,
  43. struct pci_dev *pdev, u8 stream_idx)
  44. {
  45. dev_info(&hb->dev, "Stream ID %d active at init\n", stream_id);
  46. if (!reserve_stream_id(hb, stream_id)) {
  47. dev_info(&hb->dev, "Failed to claim %s Stream ID %d\n",
  48. stream_id == PCI_IDE_RESERVED_STREAM_ID ? "reserved" :
  49. "active",
  50. stream_id);
  51. return false;
  52. }
  53. /* No stream index to reserve in the Link IDE case */
  54. if (!pdev)
  55. return true;
  56. if (!reserve_stream_index(pdev, stream_idx)) {
  57. pci_info(pdev, "Failed to claim active Selective Stream %d\n",
  58. stream_idx);
  59. return false;
  60. }
  61. return true;
  62. }
  63. void pci_ide_init(struct pci_dev *pdev)
  64. {
  65. struct pci_host_bridge *hb = pci_find_host_bridge(pdev->bus);
  66. u16 nr_link_ide, nr_ide_mem, nr_streams;
  67. u16 ide_cap;
  68. u32 val;
  69. /*
  70. * Unconditionally init so that ida idle state is consistent with
  71. * pdev->ide_cap.
  72. */
  73. ida_init(&pdev->ide_stream_ida);
  74. if (!pci_is_pcie(pdev))
  75. return;
  76. ide_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_IDE);
  77. if (!ide_cap)
  78. return;
  79. pci_read_config_dword(pdev, ide_cap + PCI_IDE_CAP, &val);
  80. if ((val & PCI_IDE_CAP_SELECTIVE) == 0)
  81. return;
  82. /*
  83. * Require endpoint IDE capability to be paired with IDE Root Port IDE
  84. * capability.
  85. */
  86. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ENDPOINT) {
  87. struct pci_dev *rp = pcie_find_root_port(pdev);
  88. if (!rp->ide_cap)
  89. return;
  90. }
  91. pdev->ide_cfg = FIELD_GET(PCI_IDE_CAP_SEL_CFG, val);
  92. pdev->ide_tee_limit = FIELD_GET(PCI_IDE_CAP_TEE_LIMITED, val);
  93. if (val & PCI_IDE_CAP_LINK)
  94. nr_link_ide = 1 + FIELD_GET(PCI_IDE_CAP_LINK_TC_NUM, val);
  95. else
  96. nr_link_ide = 0;
  97. nr_ide_mem = 0;
  98. nr_streams = 1 + FIELD_GET(PCI_IDE_CAP_SEL_NUM, val);
  99. for (u16 i = 0; i < nr_streams; i++) {
  100. int pos = __sel_ide_offset(ide_cap, nr_link_ide, i, nr_ide_mem);
  101. int nr_assoc;
  102. u32 val;
  103. u8 id;
  104. pci_read_config_dword(pdev, pos + PCI_IDE_SEL_CAP, &val);
  105. /*
  106. * Let's not entertain streams that do not have a constant
  107. * number of address association blocks
  108. */
  109. nr_assoc = FIELD_GET(PCI_IDE_SEL_CAP_ASSOC_NUM, val);
  110. if (i && (nr_assoc != nr_ide_mem)) {
  111. pci_info(pdev, "Unsupported Selective Stream %d capability, SKIP the rest\n", i);
  112. nr_streams = i;
  113. break;
  114. }
  115. nr_ide_mem = nr_assoc;
  116. /*
  117. * Claim Stream IDs and Selective Stream blocks that are already
  118. * active on the device
  119. */
  120. pci_read_config_dword(pdev, pos + PCI_IDE_SEL_CTL, &val);
  121. id = FIELD_GET(PCI_IDE_SEL_CTL_ID, val);
  122. if ((val & PCI_IDE_SEL_CTL_EN) &&
  123. !claim_stream(hb, id, pdev, i))
  124. return;
  125. }
  126. /* Reserve link stream-ids that are already active on the device */
  127. for (u16 i = 0; i < nr_link_ide; ++i) {
  128. int pos = ide_cap + PCI_IDE_LINK_STREAM_0 + i * PCI_IDE_LINK_BLOCK_SIZE;
  129. u8 id;
  130. pci_read_config_dword(pdev, pos + PCI_IDE_LINK_CTL_0, &val);
  131. id = FIELD_GET(PCI_IDE_LINK_CTL_ID, val);
  132. if ((val & PCI_IDE_LINK_CTL_EN) &&
  133. !claim_stream(hb, id, NULL, -1))
  134. return;
  135. }
  136. for (u16 i = 0; i < nr_streams; i++) {
  137. int pos = __sel_ide_offset(ide_cap, nr_link_ide, i, nr_ide_mem);
  138. pci_read_config_dword(pdev, pos + PCI_IDE_SEL_CTL, &val);
  139. if (val & PCI_IDE_SEL_CTL_EN)
  140. continue;
  141. val &= ~PCI_IDE_SEL_CTL_ID;
  142. val |= FIELD_PREP(PCI_IDE_SEL_CTL_ID, PCI_IDE_RESERVED_STREAM_ID);
  143. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_CTL, val);
  144. }
  145. for (u16 i = 0; i < nr_link_ide; ++i) {
  146. int pos = ide_cap + PCI_IDE_LINK_STREAM_0 +
  147. i * PCI_IDE_LINK_BLOCK_SIZE;
  148. pci_read_config_dword(pdev, pos, &val);
  149. if (val & PCI_IDE_LINK_CTL_EN)
  150. continue;
  151. val &= ~PCI_IDE_LINK_CTL_ID;
  152. val |= FIELD_PREP(PCI_IDE_LINK_CTL_ID, PCI_IDE_RESERVED_STREAM_ID);
  153. pci_write_config_dword(pdev, pos, val);
  154. }
  155. pdev->ide_cap = ide_cap;
  156. pdev->nr_link_ide = nr_link_ide;
  157. pdev->nr_sel_ide = nr_streams;
  158. pdev->nr_ide_mem = nr_ide_mem;
  159. }
  160. struct stream_index {
  161. struct ida *ida;
  162. u8 stream_index;
  163. };
  164. static void free_stream_index(struct stream_index *stream)
  165. {
  166. ida_free(stream->ida, stream->stream_index);
  167. }
  168. DEFINE_FREE(free_stream, struct stream_index *, if (_T) free_stream_index(_T))
  169. static struct stream_index *alloc_stream_index(struct ida *ida, u16 max,
  170. struct stream_index *stream)
  171. {
  172. int id;
  173. if (!max)
  174. return NULL;
  175. id = ida_alloc_max(ida, max - 1, GFP_KERNEL);
  176. if (id < 0)
  177. return NULL;
  178. *stream = (struct stream_index) {
  179. .ida = ida,
  180. .stream_index = id,
  181. };
  182. return stream;
  183. }
  184. /**
  185. * pci_ide_stream_alloc() - Reserve stream indices and probe for settings
  186. * @pdev: IDE capable PCIe Endpoint Physical Function
  187. *
  188. * Retrieve the Requester ID range of @pdev for programming its Root
  189. * Port IDE RID Association registers, and conversely retrieve the
  190. * Requester ID of the Root Port for programming @pdev's IDE RID
  191. * Association registers.
  192. *
  193. * Allocate a Selective IDE Stream Register Block instance per port.
  194. *
  195. * Allocate a platform stream resource from the associated host bridge.
  196. * Retrieve stream association parameters for Requester ID range and
  197. * address range restrictions for the stream.
  198. */
  199. struct pci_ide *pci_ide_stream_alloc(struct pci_dev *pdev)
  200. {
  201. /* EP, RP, + HB Stream allocation */
  202. struct stream_index __stream[PCI_IDE_HB + 1];
  203. struct pci_bus_region pref_assoc = { 0, -1 };
  204. struct pci_bus_region mem_assoc = { 0, -1 };
  205. struct resource *mem, *pref;
  206. struct pci_host_bridge *hb;
  207. struct pci_dev *rp, *br;
  208. int num_vf, rid_end;
  209. if (!pci_is_pcie(pdev))
  210. return NULL;
  211. if (pci_pcie_type(pdev) != PCI_EXP_TYPE_ENDPOINT)
  212. return NULL;
  213. if (!pdev->ide_cap)
  214. return NULL;
  215. struct pci_ide *ide __free(kfree) = kzalloc_obj(*ide);
  216. if (!ide)
  217. return NULL;
  218. hb = pci_find_host_bridge(pdev->bus);
  219. struct stream_index *hb_stream __free(free_stream) = alloc_stream_index(
  220. &hb->ide_stream_ida, hb->nr_ide_streams, &__stream[PCI_IDE_HB]);
  221. if (!hb_stream)
  222. return NULL;
  223. rp = pcie_find_root_port(pdev);
  224. struct stream_index *rp_stream __free(free_stream) = alloc_stream_index(
  225. &rp->ide_stream_ida, rp->nr_sel_ide, &__stream[PCI_IDE_RP]);
  226. if (!rp_stream)
  227. return NULL;
  228. struct stream_index *ep_stream __free(free_stream) = alloc_stream_index(
  229. &pdev->ide_stream_ida, pdev->nr_sel_ide, &__stream[PCI_IDE_EP]);
  230. if (!ep_stream)
  231. return NULL;
  232. /* for SR-IOV case, cover all VFs */
  233. num_vf = pci_num_vf(pdev);
  234. if (num_vf)
  235. rid_end = PCI_DEVID(pci_iov_virtfn_bus(pdev, num_vf - 1),
  236. pci_iov_virtfn_devfn(pdev, num_vf - 1));
  237. else
  238. rid_end = pci_dev_id(pdev);
  239. br = pci_upstream_bridge(pdev);
  240. if (!br)
  241. return NULL;
  242. /*
  243. * Check if the device consumes memory and/or prefetch-memory. Setup
  244. * downstream address association ranges for each.
  245. */
  246. mem = pci_resource_n(br, PCI_BRIDGE_MEM_WINDOW);
  247. pref = pci_resource_n(br, PCI_BRIDGE_PREF_MEM_WINDOW);
  248. if (resource_assigned(mem))
  249. pcibios_resource_to_bus(br->bus, &mem_assoc, mem);
  250. if (resource_assigned(pref))
  251. pcibios_resource_to_bus(br->bus, &pref_assoc, pref);
  252. *ide = (struct pci_ide) {
  253. .pdev = pdev,
  254. .partner = {
  255. [PCI_IDE_EP] = {
  256. .rid_start = pci_dev_id(rp),
  257. .rid_end = pci_dev_id(rp),
  258. .stream_index = no_free_ptr(ep_stream)->stream_index,
  259. /* Disable upstream address association */
  260. .mem_assoc = { 0, -1 },
  261. .pref_assoc = { 0, -1 },
  262. },
  263. [PCI_IDE_RP] = {
  264. .rid_start = pci_dev_id(pdev),
  265. .rid_end = rid_end,
  266. .stream_index = no_free_ptr(rp_stream)->stream_index,
  267. .mem_assoc = mem_assoc,
  268. .pref_assoc = pref_assoc,
  269. },
  270. },
  271. .host_bridge_stream = no_free_ptr(hb_stream)->stream_index,
  272. .stream_id = -1,
  273. };
  274. return_ptr(ide);
  275. }
  276. EXPORT_SYMBOL_GPL(pci_ide_stream_alloc);
  277. /**
  278. * pci_ide_stream_free() - unwind pci_ide_stream_alloc()
  279. * @ide: idle IDE settings descriptor
  280. *
  281. * Free all of the stream index (register block) allocations acquired by
  282. * pci_ide_stream_alloc(). The stream represented by @ide is assumed to
  283. * be unregistered and not instantiated in any device.
  284. */
  285. void pci_ide_stream_free(struct pci_ide *ide)
  286. {
  287. struct pci_dev *pdev = ide->pdev;
  288. struct pci_dev *rp = pcie_find_root_port(pdev);
  289. struct pci_host_bridge *hb = pci_find_host_bridge(pdev->bus);
  290. ida_free(&pdev->ide_stream_ida, ide->partner[PCI_IDE_EP].stream_index);
  291. ida_free(&rp->ide_stream_ida, ide->partner[PCI_IDE_RP].stream_index);
  292. ida_free(&hb->ide_stream_ida, ide->host_bridge_stream);
  293. kfree(ide);
  294. }
  295. EXPORT_SYMBOL_GPL(pci_ide_stream_free);
  296. /**
  297. * pci_ide_stream_release() - unwind and release an @ide context
  298. * @ide: partially or fully registered IDE settings descriptor
  299. *
  300. * In support of automatic cleanup of IDE setup routines perform IDE
  301. * teardown in expected reverse order of setup and with respect to which
  302. * aspects of IDE setup have successfully completed.
  303. *
  304. * Be careful that setup order mirrors this shutdown order. Otherwise,
  305. * open code releasing the IDE context.
  306. */
  307. void pci_ide_stream_release(struct pci_ide *ide)
  308. {
  309. struct pci_dev *pdev = ide->pdev;
  310. struct pci_dev *rp = pcie_find_root_port(pdev);
  311. if (ide->partner[PCI_IDE_RP].enable)
  312. pci_ide_stream_disable(rp, ide);
  313. if (ide->partner[PCI_IDE_EP].enable)
  314. pci_ide_stream_disable(pdev, ide);
  315. if (ide->partner[PCI_IDE_RP].setup)
  316. pci_ide_stream_teardown(rp, ide);
  317. if (ide->partner[PCI_IDE_EP].setup)
  318. pci_ide_stream_teardown(pdev, ide);
  319. if (ide->name)
  320. pci_ide_stream_unregister(ide);
  321. pci_ide_stream_free(ide);
  322. }
  323. EXPORT_SYMBOL_GPL(pci_ide_stream_release);
  324. struct pci_ide_stream_id {
  325. struct pci_host_bridge *hb;
  326. u8 stream_id;
  327. };
  328. static struct pci_ide_stream_id *
  329. request_stream_id(struct pci_host_bridge *hb, u8 stream_id,
  330. struct pci_ide_stream_id *sid)
  331. {
  332. if (!reserve_stream_id(hb, stream_id))
  333. return NULL;
  334. *sid = (struct pci_ide_stream_id) {
  335. .hb = hb,
  336. .stream_id = stream_id,
  337. };
  338. return sid;
  339. }
  340. DEFINE_FREE(free_stream_id, struct pci_ide_stream_id *,
  341. if (_T) ida_free(&_T->hb->ide_stream_ids_ida, _T->stream_id))
  342. /**
  343. * pci_ide_stream_register() - Prepare to activate an IDE Stream
  344. * @ide: IDE settings descriptor
  345. *
  346. * After a Stream ID has been acquired for @ide, record the presence of
  347. * the stream in sysfs. The expectation is that @ide is immutable while
  348. * registered.
  349. */
  350. int pci_ide_stream_register(struct pci_ide *ide)
  351. {
  352. struct pci_dev *pdev = ide->pdev;
  353. struct pci_host_bridge *hb = pci_find_host_bridge(pdev->bus);
  354. struct pci_ide_stream_id __sid;
  355. u8 ep_stream, rp_stream;
  356. int rc;
  357. if (ide->stream_id < 0 || ide->stream_id > U8_MAX) {
  358. pci_err(pdev, "Setup fail: Invalid Stream ID: %d\n", ide->stream_id);
  359. return -ENXIO;
  360. }
  361. struct pci_ide_stream_id *sid __free(free_stream_id) =
  362. request_stream_id(hb, ide->stream_id, &__sid);
  363. if (!sid) {
  364. pci_err(pdev, "Setup fail: Stream ID %d in use\n", ide->stream_id);
  365. return -EBUSY;
  366. }
  367. ep_stream = ide->partner[PCI_IDE_EP].stream_index;
  368. rp_stream = ide->partner[PCI_IDE_RP].stream_index;
  369. const char *name __free(kfree) = kasprintf(GFP_KERNEL, "stream%d.%d.%d",
  370. ide->host_bridge_stream,
  371. rp_stream, ep_stream);
  372. if (!name)
  373. return -ENOMEM;
  374. rc = sysfs_create_link(&hb->dev.kobj, &pdev->dev.kobj, name);
  375. if (rc)
  376. return rc;
  377. ide->name = no_free_ptr(name);
  378. /* Stream ID reservation recorded in @ide is now successfully registered */
  379. retain_and_null_ptr(sid);
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(pci_ide_stream_register);
  383. /**
  384. * pci_ide_stream_unregister() - unwind pci_ide_stream_register()
  385. * @ide: idle IDE settings descriptor
  386. *
  387. * In preparation for freeing @ide, remove sysfs enumeration for the
  388. * stream.
  389. */
  390. void pci_ide_stream_unregister(struct pci_ide *ide)
  391. {
  392. struct pci_dev *pdev = ide->pdev;
  393. struct pci_host_bridge *hb = pci_find_host_bridge(pdev->bus);
  394. sysfs_remove_link(&hb->dev.kobj, ide->name);
  395. kfree(ide->name);
  396. ida_free(&hb->ide_stream_ids_ida, ide->stream_id);
  397. ide->name = NULL;
  398. }
  399. EXPORT_SYMBOL_GPL(pci_ide_stream_unregister);
  400. static int pci_ide_domain(struct pci_dev *pdev)
  401. {
  402. if (pdev->fm_enabled)
  403. return pci_domain_nr(pdev->bus);
  404. return 0;
  405. }
  406. struct pci_ide_partner *pci_ide_to_settings(struct pci_dev *pdev, struct pci_ide *ide)
  407. {
  408. if (!pci_is_pcie(pdev)) {
  409. pci_warn_once(pdev, "not a PCIe device\n");
  410. return NULL;
  411. }
  412. switch (pci_pcie_type(pdev)) {
  413. case PCI_EXP_TYPE_ENDPOINT:
  414. if (pdev != ide->pdev) {
  415. pci_warn_once(pdev, "setup expected Endpoint: %s\n", pci_name(ide->pdev));
  416. return NULL;
  417. }
  418. return &ide->partner[PCI_IDE_EP];
  419. case PCI_EXP_TYPE_ROOT_PORT: {
  420. struct pci_dev *rp = pcie_find_root_port(ide->pdev);
  421. if (pdev != rp) {
  422. pci_warn_once(pdev, "setup expected Root Port: %s\n",
  423. pci_name(rp));
  424. return NULL;
  425. }
  426. return &ide->partner[PCI_IDE_RP];
  427. }
  428. default:
  429. pci_warn_once(pdev, "invalid device type\n");
  430. return NULL;
  431. }
  432. }
  433. EXPORT_SYMBOL_GPL(pci_ide_to_settings);
  434. static void set_ide_sel_ctl(struct pci_dev *pdev, struct pci_ide *ide,
  435. struct pci_ide_partner *settings, int pos,
  436. bool enable)
  437. {
  438. u32 val = FIELD_PREP(PCI_IDE_SEL_CTL_ID, ide->stream_id) |
  439. FIELD_PREP(PCI_IDE_SEL_CTL_DEFAULT, settings->default_stream) |
  440. FIELD_PREP(PCI_IDE_SEL_CTL_CFG_EN, pdev->ide_cfg) |
  441. FIELD_PREP(PCI_IDE_SEL_CTL_TEE_LIMITED, pdev->ide_tee_limit) |
  442. FIELD_PREP(PCI_IDE_SEL_CTL_EN, enable);
  443. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_CTL, val);
  444. }
  445. #define SEL_ADDR1_LOWER GENMASK(31, 20)
  446. #define SEL_ADDR_UPPER GENMASK_ULL(63, 32)
  447. #define PREP_PCI_IDE_SEL_ADDR1(base, limit) \
  448. (FIELD_PREP(PCI_IDE_SEL_ADDR_1_VALID, 1) | \
  449. FIELD_PREP(PCI_IDE_SEL_ADDR_1_BASE_LOW, \
  450. FIELD_GET(SEL_ADDR1_LOWER, (base))) | \
  451. FIELD_PREP(PCI_IDE_SEL_ADDR_1_LIMIT_LOW, \
  452. FIELD_GET(SEL_ADDR1_LOWER, (limit))))
  453. static void mem_assoc_to_regs(struct pci_bus_region *region,
  454. struct pci_ide_regs *regs, int idx)
  455. {
  456. /* convert to u64 range for bitfield size checks */
  457. struct range r = { region->start, region->end };
  458. regs->addr[idx].assoc1 = PREP_PCI_IDE_SEL_ADDR1(r.start, r.end);
  459. regs->addr[idx].assoc2 = FIELD_GET(SEL_ADDR_UPPER, r.end);
  460. regs->addr[idx].assoc3 = FIELD_GET(SEL_ADDR_UPPER, r.start);
  461. }
  462. /**
  463. * pci_ide_stream_to_regs() - convert IDE settings to association register values
  464. * @pdev: PCIe device object for either a Root Port or Endpoint Partner Port
  465. * @ide: registered IDE settings descriptor
  466. * @regs: output register values
  467. */
  468. static void pci_ide_stream_to_regs(struct pci_dev *pdev, struct pci_ide *ide,
  469. struct pci_ide_regs *regs)
  470. {
  471. struct pci_ide_partner *settings = pci_ide_to_settings(pdev, ide);
  472. int assoc_idx = 0;
  473. memset(regs, 0, sizeof(*regs));
  474. if (!settings)
  475. return;
  476. regs->rid1 = FIELD_PREP(PCI_IDE_SEL_RID_1_LIMIT, settings->rid_end);
  477. regs->rid2 = FIELD_PREP(PCI_IDE_SEL_RID_2_VALID, 1) |
  478. FIELD_PREP(PCI_IDE_SEL_RID_2_BASE, settings->rid_start) |
  479. FIELD_PREP(PCI_IDE_SEL_RID_2_SEG, pci_ide_domain(pdev));
  480. if (pdev->nr_ide_mem && pci_bus_region_size(&settings->mem_assoc)) {
  481. mem_assoc_to_regs(&settings->mem_assoc, regs, assoc_idx);
  482. assoc_idx++;
  483. }
  484. if (pdev->nr_ide_mem > assoc_idx &&
  485. pci_bus_region_size(&settings->pref_assoc)) {
  486. mem_assoc_to_regs(&settings->pref_assoc, regs, assoc_idx);
  487. assoc_idx++;
  488. }
  489. regs->nr_addr = assoc_idx;
  490. }
  491. /**
  492. * pci_ide_stream_setup() - program settings to Selective IDE Stream registers
  493. * @pdev: PCIe device object for either a Root Port or Endpoint Partner Port
  494. * @ide: registered IDE settings descriptor
  495. *
  496. * When @pdev is a PCI_EXP_TYPE_ENDPOINT then the PCI_IDE_EP partner
  497. * settings are written to @pdev's Selective IDE Stream register block,
  498. * and when @pdev is a PCI_EXP_TYPE_ROOT_PORT, the PCI_IDE_RP settings
  499. * are selected.
  500. */
  501. void pci_ide_stream_setup(struct pci_dev *pdev, struct pci_ide *ide)
  502. {
  503. struct pci_ide_partner *settings = pci_ide_to_settings(pdev, ide);
  504. struct pci_ide_regs regs;
  505. int pos;
  506. if (!settings)
  507. return;
  508. pci_ide_stream_to_regs(pdev, ide, &regs);
  509. pos = sel_ide_offset(pdev, settings);
  510. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_RID_1, regs.rid1);
  511. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_RID_2, regs.rid2);
  512. for (int i = 0; i < regs.nr_addr; i++) {
  513. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_1(i),
  514. regs.addr[i].assoc1);
  515. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_2(i),
  516. regs.addr[i].assoc2);
  517. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_3(i),
  518. regs.addr[i].assoc3);
  519. }
  520. /* clear extra unused address association blocks */
  521. for (int i = regs.nr_addr; i < pdev->nr_ide_mem; i++) {
  522. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_1(i), 0);
  523. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_2(i), 0);
  524. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_3(i), 0);
  525. }
  526. /*
  527. * Setup control register early for devices that expect
  528. * stream_id is set during key programming.
  529. */
  530. set_ide_sel_ctl(pdev, ide, settings, pos, false);
  531. settings->setup = 1;
  532. }
  533. EXPORT_SYMBOL_GPL(pci_ide_stream_setup);
  534. /**
  535. * pci_ide_stream_teardown() - disable the stream and clear all settings
  536. * @pdev: PCIe device object for either a Root Port or Endpoint Partner Port
  537. * @ide: registered IDE settings descriptor
  538. *
  539. * For stream destruction, zero all registers that may have been written
  540. * by pci_ide_stream_setup(). Consider pci_ide_stream_disable() to leave
  541. * settings in place while temporarily disabling the stream.
  542. */
  543. void pci_ide_stream_teardown(struct pci_dev *pdev, struct pci_ide *ide)
  544. {
  545. struct pci_ide_partner *settings = pci_ide_to_settings(pdev, ide);
  546. int pos, i;
  547. if (!settings)
  548. return;
  549. pos = sel_ide_offset(pdev, settings);
  550. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_CTL, 0);
  551. for (i = 0; i < pdev->nr_ide_mem; i++) {
  552. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_1(i), 0);
  553. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_2(i), 0);
  554. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_ADDR_3(i), 0);
  555. }
  556. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_RID_2, 0);
  557. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_RID_1, 0);
  558. settings->setup = 0;
  559. }
  560. EXPORT_SYMBOL_GPL(pci_ide_stream_teardown);
  561. /**
  562. * pci_ide_stream_enable() - enable a Selective IDE Stream
  563. * @pdev: PCIe device object for either a Root Port or Endpoint Partner Port
  564. * @ide: registered and setup IDE settings descriptor
  565. *
  566. * Activate the stream by writing to the Selective IDE Stream Control
  567. * Register.
  568. *
  569. * Return: 0 if the stream successfully entered the "secure" state, and -EINVAL
  570. * if @ide is invalid, and -ENXIO if the stream fails to enter the secure state.
  571. *
  572. * Note that the state may go "insecure" at any point after returning 0, but
  573. * those events are equivalent to a "link down" event and handled via
  574. * asynchronous error reporting.
  575. *
  576. * Caller is responsible to clear the enable bit in the -ENXIO case.
  577. */
  578. int pci_ide_stream_enable(struct pci_dev *pdev, struct pci_ide *ide)
  579. {
  580. struct pci_ide_partner *settings = pci_ide_to_settings(pdev, ide);
  581. int pos;
  582. u32 val;
  583. if (!settings)
  584. return -EINVAL;
  585. pos = sel_ide_offset(pdev, settings);
  586. set_ide_sel_ctl(pdev, ide, settings, pos, true);
  587. settings->enable = 1;
  588. pci_read_config_dword(pdev, pos + PCI_IDE_SEL_STS, &val);
  589. if (FIELD_GET(PCI_IDE_SEL_STS_STATE, val) !=
  590. PCI_IDE_SEL_STS_STATE_SECURE)
  591. return -ENXIO;
  592. return 0;
  593. }
  594. EXPORT_SYMBOL_GPL(pci_ide_stream_enable);
  595. /**
  596. * pci_ide_stream_disable() - disable a Selective IDE Stream
  597. * @pdev: PCIe device object for either a Root Port or Endpoint Partner Port
  598. * @ide: registered and setup IDE settings descriptor
  599. *
  600. * Clear the Selective IDE Stream Control Register, but leave all other
  601. * registers untouched.
  602. */
  603. void pci_ide_stream_disable(struct pci_dev *pdev, struct pci_ide *ide)
  604. {
  605. struct pci_ide_partner *settings = pci_ide_to_settings(pdev, ide);
  606. int pos;
  607. if (!settings)
  608. return;
  609. pos = sel_ide_offset(pdev, settings);
  610. pci_write_config_dword(pdev, pos + PCI_IDE_SEL_CTL, 0);
  611. settings->enable = 0;
  612. }
  613. EXPORT_SYMBOL_GPL(pci_ide_stream_disable);
  614. void pci_ide_init_host_bridge(struct pci_host_bridge *hb)
  615. {
  616. hb->nr_ide_streams = 256;
  617. ida_init(&hb->ide_stream_ida);
  618. ida_init(&hb->ide_stream_ids_ida);
  619. reserve_stream_id(hb, PCI_IDE_RESERVED_STREAM_ID);
  620. }
  621. static ssize_t available_secure_streams_show(struct device *dev,
  622. struct device_attribute *attr,
  623. char *buf)
  624. {
  625. struct pci_host_bridge *hb = to_pci_host_bridge(dev);
  626. int nr = READ_ONCE(hb->nr_ide_streams);
  627. int avail = nr;
  628. if (!nr)
  629. return -ENXIO;
  630. /*
  631. * Yes, this is inefficient and racy, but it is only for occasional
  632. * platform resource surveys. Worst case is bounded to 256 streams.
  633. */
  634. for (int i = 0; i < nr; i++)
  635. if (ida_exists(&hb->ide_stream_ida, i))
  636. avail--;
  637. return sysfs_emit(buf, "%d\n", avail);
  638. }
  639. static DEVICE_ATTR_RO(available_secure_streams);
  640. static struct attribute *pci_ide_attrs[] = {
  641. &dev_attr_available_secure_streams.attr,
  642. NULL
  643. };
  644. static umode_t pci_ide_attr_visible(struct kobject *kobj, struct attribute *a, int n)
  645. {
  646. struct device *dev = kobj_to_dev(kobj);
  647. struct pci_host_bridge *hb = to_pci_host_bridge(dev);
  648. if (a == &dev_attr_available_secure_streams.attr)
  649. if (!hb->nr_ide_streams)
  650. return 0;
  651. return a->mode;
  652. }
  653. const struct attribute_group pci_ide_attr_group = {
  654. .attrs = pci_ide_attrs,
  655. .is_visible = pci_ide_attr_visible,
  656. };
  657. /**
  658. * pci_ide_set_nr_streams() - sets size of the pool of IDE Stream resources
  659. * @hb: host bridge boundary for the stream pool
  660. * @nr: number of streams
  661. *
  662. * Platform PCI init and/or expert test module use only. Limit IDE
  663. * Stream establishment by setting the number of stream resources
  664. * available at the host bridge. Platform init code must set this before
  665. * the first pci_ide_stream_alloc() call if the platform has less than the
  666. * default of 256 streams per host-bridge.
  667. *
  668. * The "PCI_IDE" symbol namespace is required because this is typically
  669. * a detail that is settled in early PCI init. I.e. this export is not
  670. * for endpoint drivers.
  671. */
  672. void pci_ide_set_nr_streams(struct pci_host_bridge *hb, u16 nr)
  673. {
  674. hb->nr_ide_streams = min(nr, 256);
  675. WARN_ON_ONCE(!ida_is_empty(&hb->ide_stream_ida));
  676. sysfs_update_group(&hb->dev.kobj, &pci_ide_attr_group);
  677. }
  678. EXPORT_SYMBOL_NS_GPL(pci_ide_set_nr_streams, "PCI_IDE");
  679. void pci_ide_destroy(struct pci_dev *pdev)
  680. {
  681. ida_destroy(&pdev->ide_stream_ida);
  682. }