doe.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Data Object Exchange
  4. * PCIe r6.0, sec 6.30 DOE
  5. *
  6. * Copyright (C) 2021 Huawei
  7. * Jonathan Cameron <Jonathan.Cameron@huawei.com>
  8. *
  9. * Copyright (C) 2022 Intel Corporation
  10. * Ira Weiny <ira.weiny@intel.com>
  11. */
  12. #define dev_fmt(fmt) "DOE: " fmt
  13. #include <linux/bitfield.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/mutex.h>
  18. #include <linux/pci.h>
  19. #include <linux/pci-doe.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/workqueue.h>
  22. #include "pci.h"
  23. /* Timeout of 1 second from 6.30.2 Operation, PCI Spec r6.0 */
  24. #define PCI_DOE_TIMEOUT HZ
  25. #define PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128)
  26. #define PCI_DOE_FLAG_CANCEL 0
  27. #define PCI_DOE_FLAG_DEAD 1
  28. /* Max data object length is 2^18 dwords */
  29. #define PCI_DOE_MAX_LENGTH (1 << 18)
  30. /**
  31. * struct pci_doe_mb - State for a single DOE mailbox
  32. *
  33. * This state is used to manage a single DOE mailbox capability. All fields
  34. * should be considered opaque to the consumers and the structure passed into
  35. * the helpers below after being created by pci_doe_create_mb().
  36. *
  37. * @pdev: PCI device this mailbox belongs to
  38. * @cap_offset: Capability offset
  39. * @feats: Array of features supported (encoded as long values)
  40. * @wq: Wait queue for work item
  41. * @work_queue: Queue of pci_doe_work items
  42. * @flags: Bit array of PCI_DOE_FLAG_* flags
  43. * @sysfs_attrs: Array of sysfs device attributes
  44. */
  45. struct pci_doe_mb {
  46. struct pci_dev *pdev;
  47. u16 cap_offset;
  48. struct xarray feats;
  49. wait_queue_head_t wq;
  50. struct workqueue_struct *work_queue;
  51. unsigned long flags;
  52. #ifdef CONFIG_SYSFS
  53. struct device_attribute *sysfs_attrs;
  54. #endif
  55. };
  56. struct pci_doe_feature {
  57. u16 vid;
  58. u8 type;
  59. };
  60. /**
  61. * struct pci_doe_task - represents a single query/response
  62. *
  63. * @feat: DOE Feature
  64. * @request_pl: The request payload
  65. * @request_pl_sz: Size of the request payload (bytes)
  66. * @response_pl: The response payload
  67. * @response_pl_sz: Size of the response payload (bytes)
  68. * @rv: Return value. Length of received response or error (bytes)
  69. * @complete: Called when task is complete
  70. * @private: Private data for the consumer
  71. * @work: Used internally by the mailbox
  72. * @doe_mb: Used internally by the mailbox
  73. */
  74. struct pci_doe_task {
  75. struct pci_doe_feature feat;
  76. const __le32 *request_pl;
  77. size_t request_pl_sz;
  78. __le32 *response_pl;
  79. size_t response_pl_sz;
  80. int rv;
  81. void (*complete)(struct pci_doe_task *task);
  82. void *private;
  83. /* initialized by pci_doe_submit_task() */
  84. struct work_struct work;
  85. struct pci_doe_mb *doe_mb;
  86. };
  87. #ifdef CONFIG_SYSFS
  88. static ssize_t doe_discovery_show(struct device *dev,
  89. struct device_attribute *attr,
  90. char *buf)
  91. {
  92. return sysfs_emit(buf, "0001:00\n");
  93. }
  94. static DEVICE_ATTR_RO(doe_discovery);
  95. static struct attribute *pci_doe_sysfs_feature_attrs[] = {
  96. &dev_attr_doe_discovery.attr,
  97. NULL
  98. };
  99. static bool pci_doe_features_sysfs_group_visible(struct kobject *kobj)
  100. {
  101. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  102. return !xa_empty(&pdev->doe_mbs);
  103. }
  104. DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(pci_doe_features_sysfs)
  105. const struct attribute_group pci_doe_sysfs_group = {
  106. .name = "doe_features",
  107. .attrs = pci_doe_sysfs_feature_attrs,
  108. .is_visible = SYSFS_GROUP_VISIBLE(pci_doe_features_sysfs),
  109. };
  110. static ssize_t pci_doe_sysfs_feature_show(struct device *dev,
  111. struct device_attribute *attr,
  112. char *buf)
  113. {
  114. return sysfs_emit(buf, "%s\n", attr->attr.name);
  115. }
  116. static void pci_doe_sysfs_feature_remove(struct pci_dev *pdev,
  117. struct pci_doe_mb *doe_mb)
  118. {
  119. struct device_attribute *attrs = doe_mb->sysfs_attrs;
  120. struct device *dev = &pdev->dev;
  121. unsigned long i;
  122. void *entry;
  123. if (!attrs)
  124. return;
  125. doe_mb->sysfs_attrs = NULL;
  126. xa_for_each(&doe_mb->feats, i, entry) {
  127. if (attrs[i].show)
  128. sysfs_remove_file_from_group(&dev->kobj, &attrs[i].attr,
  129. pci_doe_sysfs_group.name);
  130. kfree(attrs[i].attr.name);
  131. }
  132. kfree(attrs);
  133. }
  134. static int pci_doe_sysfs_feature_populate(struct pci_dev *pdev,
  135. struct pci_doe_mb *doe_mb)
  136. {
  137. struct device *dev = &pdev->dev;
  138. struct device_attribute *attrs;
  139. unsigned long num_features = 0;
  140. unsigned long vid, type;
  141. unsigned long i;
  142. void *entry;
  143. int ret;
  144. xa_for_each(&doe_mb->feats, i, entry)
  145. num_features++;
  146. attrs = kzalloc_objs(*attrs, num_features);
  147. if (!attrs) {
  148. pci_warn(pdev, "Failed allocating the device_attribute array\n");
  149. return -ENOMEM;
  150. }
  151. doe_mb->sysfs_attrs = attrs;
  152. xa_for_each(&doe_mb->feats, i, entry) {
  153. sysfs_attr_init(&attrs[i].attr);
  154. vid = xa_to_value(entry) >> 8;
  155. type = xa_to_value(entry) & 0xFF;
  156. if (vid == PCI_VENDOR_ID_PCI_SIG &&
  157. type == PCI_DOE_FEATURE_DISCOVERY) {
  158. /*
  159. * DOE Discovery, manually displayed by
  160. * `dev_attr_doe_discovery`
  161. */
  162. continue;
  163. }
  164. attrs[i].attr.name = kasprintf(GFP_KERNEL,
  165. "%04lx:%02lx", vid, type);
  166. if (!attrs[i].attr.name) {
  167. ret = -ENOMEM;
  168. pci_warn(pdev, "Failed allocating the attribute name\n");
  169. goto fail;
  170. }
  171. attrs[i].attr.mode = 0444;
  172. attrs[i].show = pci_doe_sysfs_feature_show;
  173. ret = sysfs_add_file_to_group(&dev->kobj, &attrs[i].attr,
  174. pci_doe_sysfs_group.name);
  175. if (ret) {
  176. attrs[i].show = NULL;
  177. if (ret != -EEXIST) {
  178. pci_warn(pdev, "Failed adding %s to sysfs group\n",
  179. attrs[i].attr.name);
  180. goto fail;
  181. } else
  182. kfree(attrs[i].attr.name);
  183. }
  184. }
  185. return 0;
  186. fail:
  187. pci_doe_sysfs_feature_remove(pdev, doe_mb);
  188. return ret;
  189. }
  190. void pci_doe_sysfs_teardown(struct pci_dev *pdev)
  191. {
  192. struct pci_doe_mb *doe_mb;
  193. unsigned long index;
  194. xa_for_each(&pdev->doe_mbs, index, doe_mb)
  195. pci_doe_sysfs_feature_remove(pdev, doe_mb);
  196. }
  197. void pci_doe_sysfs_init(struct pci_dev *pdev)
  198. {
  199. struct pci_doe_mb *doe_mb;
  200. unsigned long index;
  201. int ret;
  202. xa_for_each(&pdev->doe_mbs, index, doe_mb) {
  203. ret = pci_doe_sysfs_feature_populate(pdev, doe_mb);
  204. if (ret)
  205. return;
  206. }
  207. }
  208. #endif
  209. static int pci_doe_wait(struct pci_doe_mb *doe_mb, unsigned long timeout)
  210. {
  211. if (wait_event_timeout(doe_mb->wq,
  212. test_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags),
  213. timeout))
  214. return -EIO;
  215. return 0;
  216. }
  217. static void pci_doe_write_ctrl(struct pci_doe_mb *doe_mb, u32 val)
  218. {
  219. struct pci_dev *pdev = doe_mb->pdev;
  220. int offset = doe_mb->cap_offset;
  221. pci_write_config_dword(pdev, offset + PCI_DOE_CTRL, val);
  222. }
  223. static int pci_doe_abort(struct pci_doe_mb *doe_mb)
  224. {
  225. struct pci_dev *pdev = doe_mb->pdev;
  226. int offset = doe_mb->cap_offset;
  227. unsigned long timeout_jiffies;
  228. pci_dbg(pdev, "[%x] Issuing Abort\n", offset);
  229. timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
  230. pci_doe_write_ctrl(doe_mb, PCI_DOE_CTRL_ABORT);
  231. do {
  232. int rc;
  233. u32 val;
  234. rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
  235. if (rc)
  236. return rc;
  237. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  238. /* Abort success! */
  239. if (!FIELD_GET(PCI_DOE_STATUS_ERROR, val) &&
  240. !FIELD_GET(PCI_DOE_STATUS_BUSY, val))
  241. return 0;
  242. } while (!time_after(jiffies, timeout_jiffies));
  243. /* Abort has timed out and the MB is dead */
  244. pci_err(pdev, "[%x] ABORT timed out\n", offset);
  245. return -EIO;
  246. }
  247. static int pci_doe_send_req(struct pci_doe_mb *doe_mb,
  248. struct pci_doe_task *task)
  249. {
  250. struct pci_dev *pdev = doe_mb->pdev;
  251. int offset = doe_mb->cap_offset;
  252. unsigned long timeout_jiffies;
  253. size_t length, remainder;
  254. u32 val;
  255. int i;
  256. /*
  257. * Check the DOE busy bit is not set. If it is set, this could indicate
  258. * someone other than Linux (e.g. firmware) is using the mailbox. Note
  259. * it is expected that firmware and OS will negotiate access rights via
  260. * an, as yet to be defined, method.
  261. *
  262. * Wait up to one PCI_DOE_TIMEOUT period to allow the prior command to
  263. * finish. Otherwise, simply error out as unable to field the request.
  264. *
  265. * PCIe r6.2 sec 6.30.3 states no interrupt is raised when the DOE Busy
  266. * bit is cleared, so polling here is our best option for the moment.
  267. */
  268. timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
  269. do {
  270. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  271. } while (FIELD_GET(PCI_DOE_STATUS_BUSY, val) &&
  272. !time_after(jiffies, timeout_jiffies));
  273. if (FIELD_GET(PCI_DOE_STATUS_BUSY, val))
  274. return -EBUSY;
  275. if (FIELD_GET(PCI_DOE_STATUS_ERROR, val))
  276. return -EIO;
  277. /* Length is 2 DW of header + length of payload in DW */
  278. length = 2 + DIV_ROUND_UP(task->request_pl_sz, sizeof(__le32));
  279. if (length > PCI_DOE_MAX_LENGTH)
  280. return -EIO;
  281. if (length == PCI_DOE_MAX_LENGTH)
  282. length = 0;
  283. /* Write DOE Header */
  284. val = FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_VID, task->feat.vid) |
  285. FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, task->feat.type);
  286. pci_write_config_dword(pdev, offset + PCI_DOE_WRITE, val);
  287. pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
  288. FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH,
  289. length));
  290. /* Write payload */
  291. for (i = 0; i < task->request_pl_sz / sizeof(__le32); i++)
  292. pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
  293. le32_to_cpu(task->request_pl[i]));
  294. /* Write last payload dword */
  295. remainder = task->request_pl_sz % sizeof(__le32);
  296. if (remainder) {
  297. val = 0;
  298. memcpy(&val, &task->request_pl[i], remainder);
  299. le32_to_cpus(&val);
  300. pci_write_config_dword(pdev, offset + PCI_DOE_WRITE, val);
  301. }
  302. pci_doe_write_ctrl(doe_mb, PCI_DOE_CTRL_GO);
  303. return 0;
  304. }
  305. static bool pci_doe_data_obj_ready(struct pci_doe_mb *doe_mb)
  306. {
  307. struct pci_dev *pdev = doe_mb->pdev;
  308. int offset = doe_mb->cap_offset;
  309. u32 val;
  310. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  311. if (FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val))
  312. return true;
  313. return false;
  314. }
  315. static int pci_doe_recv_resp(struct pci_doe_mb *doe_mb, struct pci_doe_task *task)
  316. {
  317. size_t length, payload_length, remainder, received;
  318. struct pci_dev *pdev = doe_mb->pdev;
  319. int offset = doe_mb->cap_offset;
  320. int i = 0;
  321. u32 val;
  322. /* Read the first dword to get the feature */
  323. pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
  324. if ((FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_VID, val) != task->feat.vid) ||
  325. (FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, val) != task->feat.type)) {
  326. dev_err_ratelimited(&pdev->dev, "[%x] expected [VID, Feature] = [%04x, %02x], got [%04x, %02x]\n",
  327. doe_mb->cap_offset, task->feat.vid, task->feat.type,
  328. FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_VID, val),
  329. FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, val));
  330. return -EIO;
  331. }
  332. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  333. /* Read the second dword to get the length */
  334. pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
  335. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  336. length = FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH, val);
  337. /* A value of 0x0 indicates max data object length */
  338. if (!length)
  339. length = PCI_DOE_MAX_LENGTH;
  340. if (length < 2)
  341. return -EIO;
  342. /* First 2 dwords have already been read */
  343. length -= 2;
  344. received = task->response_pl_sz;
  345. payload_length = DIV_ROUND_UP(task->response_pl_sz, sizeof(__le32));
  346. remainder = task->response_pl_sz % sizeof(__le32);
  347. /* remainder signifies number of data bytes in last payload dword */
  348. if (!remainder)
  349. remainder = sizeof(__le32);
  350. if (length < payload_length) {
  351. received = length * sizeof(__le32);
  352. payload_length = length;
  353. remainder = sizeof(__le32);
  354. }
  355. if (payload_length) {
  356. /* Read all payload dwords except the last */
  357. for (; i < payload_length - 1; i++) {
  358. pci_read_config_dword(pdev, offset + PCI_DOE_READ,
  359. &val);
  360. task->response_pl[i] = cpu_to_le32(val);
  361. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  362. }
  363. /* Read last payload dword */
  364. pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
  365. cpu_to_le32s(&val);
  366. memcpy(&task->response_pl[i], &val, remainder);
  367. /* Prior to the last ack, ensure Data Object Ready */
  368. if (!pci_doe_data_obj_ready(doe_mb))
  369. return -EIO;
  370. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  371. i++;
  372. }
  373. /* Flush excess length */
  374. for (; i < length; i++) {
  375. pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
  376. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  377. }
  378. /* Final error check to pick up on any since Data Object Ready */
  379. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  380. if (FIELD_GET(PCI_DOE_STATUS_ERROR, val))
  381. return -EIO;
  382. return received;
  383. }
  384. static void signal_task_complete(struct pci_doe_task *task, int rv)
  385. {
  386. task->rv = rv;
  387. destroy_work_on_stack(&task->work);
  388. task->complete(task);
  389. }
  390. static void signal_task_abort(struct pci_doe_task *task, int rv)
  391. {
  392. struct pci_doe_mb *doe_mb = task->doe_mb;
  393. struct pci_dev *pdev = doe_mb->pdev;
  394. if (pci_doe_abort(doe_mb)) {
  395. /*
  396. * If the device can't process an abort; set the mailbox dead
  397. * - no more submissions
  398. */
  399. pci_err(pdev, "[%x] Abort failed marking mailbox dead\n",
  400. doe_mb->cap_offset);
  401. set_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags);
  402. }
  403. signal_task_complete(task, rv);
  404. }
  405. static void doe_statemachine_work(struct work_struct *work)
  406. {
  407. struct pci_doe_task *task = container_of(work, struct pci_doe_task,
  408. work);
  409. struct pci_doe_mb *doe_mb = task->doe_mb;
  410. struct pci_dev *pdev = doe_mb->pdev;
  411. int offset = doe_mb->cap_offset;
  412. unsigned long timeout_jiffies;
  413. u32 val;
  414. int rc;
  415. if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags)) {
  416. signal_task_complete(task, -EIO);
  417. return;
  418. }
  419. /* Send request */
  420. rc = pci_doe_send_req(doe_mb, task);
  421. if (rc) {
  422. /*
  423. * The specification does not provide any guidance on how to
  424. * resolve conflicting requests from other entities.
  425. * Furthermore, it is likely that busy will not be detected
  426. * most of the time. Flag any detection of status busy with an
  427. * error.
  428. */
  429. if (rc == -EBUSY)
  430. dev_err_ratelimited(&pdev->dev, "[%x] busy detected; another entity is sending conflicting requests\n",
  431. offset);
  432. signal_task_abort(task, rc);
  433. return;
  434. }
  435. timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
  436. /* Poll for response */
  437. retry_resp:
  438. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  439. if (FIELD_GET(PCI_DOE_STATUS_ERROR, val)) {
  440. signal_task_abort(task, -EIO);
  441. return;
  442. }
  443. if (!FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val)) {
  444. if (time_after(jiffies, timeout_jiffies)) {
  445. signal_task_abort(task, -EIO);
  446. return;
  447. }
  448. rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
  449. if (rc) {
  450. signal_task_abort(task, rc);
  451. return;
  452. }
  453. goto retry_resp;
  454. }
  455. rc = pci_doe_recv_resp(doe_mb, task);
  456. if (rc < 0) {
  457. signal_task_abort(task, rc);
  458. return;
  459. }
  460. signal_task_complete(task, rc);
  461. }
  462. static void pci_doe_task_complete(struct pci_doe_task *task)
  463. {
  464. complete(task->private);
  465. }
  466. static int pci_doe_discovery(struct pci_doe_mb *doe_mb, u8 capver, u8 *index, u16 *vid,
  467. u8 *feature)
  468. {
  469. u32 request_pl = FIELD_PREP(PCI_DOE_DATA_OBJECT_DISC_REQ_3_INDEX,
  470. *index) |
  471. FIELD_PREP(PCI_DOE_DATA_OBJECT_DISC_REQ_3_VER,
  472. (capver >= 2) ? 2 : 0);
  473. __le32 request_pl_le = cpu_to_le32(request_pl);
  474. __le32 response_pl_le;
  475. u32 response_pl;
  476. int rc;
  477. rc = pci_doe(doe_mb, PCI_VENDOR_ID_PCI_SIG, PCI_DOE_FEATURE_DISCOVERY,
  478. &request_pl_le, sizeof(request_pl_le),
  479. &response_pl_le, sizeof(response_pl_le));
  480. if (rc < 0)
  481. return rc;
  482. if (rc != sizeof(response_pl_le))
  483. return -EIO;
  484. response_pl = le32_to_cpu(response_pl_le);
  485. *vid = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_VID, response_pl);
  486. *feature = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_TYPE,
  487. response_pl);
  488. *index = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX,
  489. response_pl);
  490. return 0;
  491. }
  492. static void *pci_doe_xa_feat_entry(u16 vid, u8 type)
  493. {
  494. return xa_mk_value((vid << 8) | type);
  495. }
  496. static int pci_doe_cache_features(struct pci_doe_mb *doe_mb)
  497. {
  498. u8 index = 0;
  499. u8 xa_idx = 0;
  500. u32 hdr = 0;
  501. pci_read_config_dword(doe_mb->pdev, doe_mb->cap_offset, &hdr);
  502. do {
  503. int rc;
  504. u16 vid;
  505. u8 type;
  506. rc = pci_doe_discovery(doe_mb, PCI_EXT_CAP_VER(hdr), &index,
  507. &vid, &type);
  508. if (rc)
  509. return rc;
  510. pci_dbg(doe_mb->pdev,
  511. "[%x] Found feature %d vid: %x type: %x\n",
  512. doe_mb->cap_offset, xa_idx, vid, type);
  513. rc = xa_insert(&doe_mb->feats, xa_idx++,
  514. pci_doe_xa_feat_entry(vid, type), GFP_KERNEL);
  515. if (rc)
  516. return rc;
  517. } while (index);
  518. return 0;
  519. }
  520. static void pci_doe_cancel_tasks(struct pci_doe_mb *doe_mb)
  521. {
  522. /* Stop all pending work items from starting */
  523. set_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags);
  524. /* Cancel an in progress work item, if necessary */
  525. set_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags);
  526. wake_up(&doe_mb->wq);
  527. }
  528. /**
  529. * pci_doe_create_mb() - Create a DOE mailbox object
  530. *
  531. * @pdev: PCI device to create the DOE mailbox for
  532. * @cap_offset: Offset of the DOE mailbox
  533. *
  534. * Create a single mailbox object to manage the mailbox feature at the
  535. * cap_offset specified.
  536. *
  537. * RETURNS: created mailbox object on success
  538. * ERR_PTR(-errno) on failure
  539. */
  540. static struct pci_doe_mb *pci_doe_create_mb(struct pci_dev *pdev,
  541. u16 cap_offset)
  542. {
  543. struct pci_doe_mb *doe_mb;
  544. int rc;
  545. doe_mb = kzalloc_obj(*doe_mb);
  546. if (!doe_mb)
  547. return ERR_PTR(-ENOMEM);
  548. doe_mb->pdev = pdev;
  549. doe_mb->cap_offset = cap_offset;
  550. init_waitqueue_head(&doe_mb->wq);
  551. xa_init(&doe_mb->feats);
  552. doe_mb->work_queue = alloc_ordered_workqueue("%s %s DOE [%x]", 0,
  553. dev_bus_name(&pdev->dev),
  554. pci_name(pdev),
  555. doe_mb->cap_offset);
  556. if (!doe_mb->work_queue) {
  557. pci_err(pdev, "[%x] failed to allocate work queue\n",
  558. doe_mb->cap_offset);
  559. rc = -ENOMEM;
  560. goto err_free;
  561. }
  562. /* Reset the mailbox by issuing an abort */
  563. rc = pci_doe_abort(doe_mb);
  564. if (rc) {
  565. pci_err(pdev, "[%x] failed to reset mailbox with abort command : %d\n",
  566. doe_mb->cap_offset, rc);
  567. goto err_destroy_wq;
  568. }
  569. /*
  570. * The state machine and the mailbox should be in sync now;
  571. * Use the mailbox to query features.
  572. */
  573. rc = pci_doe_cache_features(doe_mb);
  574. if (rc) {
  575. pci_err(pdev, "[%x] failed to cache features : %d\n",
  576. doe_mb->cap_offset, rc);
  577. goto err_cancel;
  578. }
  579. return doe_mb;
  580. err_cancel:
  581. pci_doe_cancel_tasks(doe_mb);
  582. xa_destroy(&doe_mb->feats);
  583. err_destroy_wq:
  584. destroy_workqueue(doe_mb->work_queue);
  585. err_free:
  586. kfree(doe_mb);
  587. return ERR_PTR(rc);
  588. }
  589. /**
  590. * pci_doe_destroy_mb() - Destroy a DOE mailbox object
  591. *
  592. * @doe_mb: DOE mailbox
  593. *
  594. * Destroy all internal data structures created for the DOE mailbox.
  595. */
  596. static void pci_doe_destroy_mb(struct pci_doe_mb *doe_mb)
  597. {
  598. pci_doe_cancel_tasks(doe_mb);
  599. xa_destroy(&doe_mb->feats);
  600. destroy_workqueue(doe_mb->work_queue);
  601. kfree(doe_mb);
  602. }
  603. /**
  604. * pci_doe_supports_feat() - Return if the DOE instance supports the given
  605. * feature
  606. * @doe_mb: DOE mailbox capability to query
  607. * @vid: Feature Vendor ID
  608. * @type: Feature type
  609. *
  610. * RETURNS: True if the DOE mailbox supports the feature specified
  611. */
  612. static bool pci_doe_supports_feat(struct pci_doe_mb *doe_mb, u16 vid, u8 type)
  613. {
  614. unsigned long index;
  615. void *entry;
  616. /* The discovery feature must always be supported */
  617. if (vid == PCI_VENDOR_ID_PCI_SIG && type == PCI_DOE_FEATURE_DISCOVERY)
  618. return true;
  619. xa_for_each(&doe_mb->feats, index, entry)
  620. if (entry == pci_doe_xa_feat_entry(vid, type))
  621. return true;
  622. return false;
  623. }
  624. /**
  625. * pci_doe_submit_task() - Submit a task to be processed by the state machine
  626. *
  627. * @doe_mb: DOE mailbox capability to submit to
  628. * @task: task to be queued
  629. *
  630. * Submit a DOE task (request/response) to the DOE mailbox to be processed.
  631. * Returns upon queueing the task object. If the queue is full this function
  632. * will sleep until there is room in the queue.
  633. *
  634. * task->complete will be called when the state machine is done processing this
  635. * task.
  636. *
  637. * @task must be allocated on the stack.
  638. *
  639. * Excess data will be discarded.
  640. *
  641. * RETURNS: 0 when task has been successfully queued, -ERRNO on error
  642. */
  643. static int pci_doe_submit_task(struct pci_doe_mb *doe_mb,
  644. struct pci_doe_task *task)
  645. {
  646. if (!pci_doe_supports_feat(doe_mb, task->feat.vid, task->feat.type))
  647. return -EINVAL;
  648. if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags))
  649. return -EIO;
  650. task->doe_mb = doe_mb;
  651. INIT_WORK_ONSTACK(&task->work, doe_statemachine_work);
  652. queue_work(doe_mb->work_queue, &task->work);
  653. return 0;
  654. }
  655. /**
  656. * pci_doe() - Perform Data Object Exchange
  657. *
  658. * @doe_mb: DOE Mailbox
  659. * @vendor: Vendor ID
  660. * @type: Data Object Type
  661. * @request: Request payload
  662. * @request_sz: Size of request payload (bytes)
  663. * @response: Response payload
  664. * @response_sz: Size of response payload (bytes)
  665. *
  666. * Submit @request to @doe_mb and store the @response.
  667. * The DOE exchange is performed synchronously and may therefore sleep.
  668. *
  669. * Payloads are treated as opaque byte streams which are transmitted verbatim,
  670. * without byte-swapping. If payloads contain little-endian register values,
  671. * the caller is responsible for conversion with cpu_to_le32() / le32_to_cpu().
  672. *
  673. * For convenience, arbitrary payload sizes are allowed even though PCIe r6.0
  674. * sec 6.30.1 specifies the Data Object Header 2 "Length" in dwords. The last
  675. * (partial) dword is copied with byte granularity and padded with zeroes if
  676. * necessary. Callers are thus relieved of using dword-sized bounce buffers.
  677. *
  678. * RETURNS: Length of received response or negative errno.
  679. * Received data in excess of @response_sz is discarded.
  680. * The length may be smaller than @response_sz and the caller
  681. * is responsible for checking that.
  682. */
  683. int pci_doe(struct pci_doe_mb *doe_mb, u16 vendor, u8 type,
  684. const void *request, size_t request_sz,
  685. void *response, size_t response_sz)
  686. {
  687. DECLARE_COMPLETION_ONSTACK(c);
  688. struct pci_doe_task task = {
  689. .feat.vid = vendor,
  690. .feat.type = type,
  691. .request_pl = request,
  692. .request_pl_sz = request_sz,
  693. .response_pl = response,
  694. .response_pl_sz = response_sz,
  695. .complete = pci_doe_task_complete,
  696. .private = &c,
  697. };
  698. int rc;
  699. rc = pci_doe_submit_task(doe_mb, &task);
  700. if (rc)
  701. return rc;
  702. wait_for_completion(&c);
  703. return task.rv;
  704. }
  705. EXPORT_SYMBOL_GPL(pci_doe);
  706. /**
  707. * pci_find_doe_mailbox() - Find Data Object Exchange mailbox
  708. *
  709. * @pdev: PCI device
  710. * @vendor: Vendor ID
  711. * @type: Data Object Type
  712. *
  713. * Find first DOE mailbox of a PCI device which supports the given feature.
  714. *
  715. * RETURNS: Pointer to the DOE mailbox or NULL if none was found.
  716. */
  717. struct pci_doe_mb *pci_find_doe_mailbox(struct pci_dev *pdev, u16 vendor,
  718. u8 type)
  719. {
  720. struct pci_doe_mb *doe_mb;
  721. unsigned long index;
  722. xa_for_each(&pdev->doe_mbs, index, doe_mb)
  723. if (pci_doe_supports_feat(doe_mb, vendor, type))
  724. return doe_mb;
  725. return NULL;
  726. }
  727. EXPORT_SYMBOL_GPL(pci_find_doe_mailbox);
  728. void pci_doe_init(struct pci_dev *pdev)
  729. {
  730. struct pci_doe_mb *doe_mb;
  731. u16 offset = 0;
  732. int rc;
  733. xa_init(&pdev->doe_mbs);
  734. while ((offset = pci_find_next_ext_capability(pdev, offset,
  735. PCI_EXT_CAP_ID_DOE))) {
  736. doe_mb = pci_doe_create_mb(pdev, offset);
  737. if (IS_ERR(doe_mb)) {
  738. pci_err(pdev, "[%x] failed to create mailbox: %ld\n",
  739. offset, PTR_ERR(doe_mb));
  740. continue;
  741. }
  742. rc = xa_insert(&pdev->doe_mbs, offset, doe_mb, GFP_KERNEL);
  743. if (rc) {
  744. pci_err(pdev, "[%x] failed to insert mailbox: %d\n",
  745. offset, rc);
  746. pci_doe_destroy_mb(doe_mb);
  747. }
  748. }
  749. }
  750. void pci_doe_destroy(struct pci_dev *pdev)
  751. {
  752. struct pci_doe_mb *doe_mb;
  753. unsigned long index;
  754. xa_for_each(&pdev->doe_mbs, index, doe_mb)
  755. pci_doe_destroy_mb(doe_mb);
  756. xa_destroy(&pdev->doe_mbs);
  757. }
  758. void pci_doe_disconnected(struct pci_dev *pdev)
  759. {
  760. struct pci_doe_mb *doe_mb;
  761. unsigned long index;
  762. xa_for_each(&pdev->doe_mbs, index, doe_mb)
  763. pci_doe_cancel_tasks(doe_mb);
  764. }