doe.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 <[email protected]>
  8. *
  9. * Copyright (C) 2022 Intel Corporation
  10. * Ira Weiny <[email protected]>
  11. */
  12. #define dev_fmt(fmt) "DOE: " fmt
  13. #include <linux/bitfield.h>
  14. #include <linux/delay.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/mutex.h>
  17. #include <linux/pci.h>
  18. #include <linux/pci-doe.h>
  19. #include <linux/workqueue.h>
  20. #define PCI_DOE_PROTOCOL_DISCOVERY 0
  21. /* Timeout of 1 second from 6.30.2 Operation, PCI Spec r6.0 */
  22. #define PCI_DOE_TIMEOUT HZ
  23. #define PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128)
  24. #define PCI_DOE_FLAG_CANCEL 0
  25. #define PCI_DOE_FLAG_DEAD 1
  26. /* Max data object length is 2^18 dwords */
  27. #define PCI_DOE_MAX_LENGTH (1 << 18)
  28. /**
  29. * struct pci_doe_mb - State for a single DOE mailbox
  30. *
  31. * This state is used to manage a single DOE mailbox capability. All fields
  32. * should be considered opaque to the consumers and the structure passed into
  33. * the helpers below after being created by devm_pci_doe_create()
  34. *
  35. * @pdev: PCI device this mailbox belongs to
  36. * @cap_offset: Capability offset
  37. * @prots: Array of protocols supported (encoded as long values)
  38. * @wq: Wait queue for work item
  39. * @work_queue: Queue of pci_doe_work items
  40. * @flags: Bit array of PCI_DOE_FLAG_* flags
  41. */
  42. struct pci_doe_mb {
  43. struct pci_dev *pdev;
  44. u16 cap_offset;
  45. struct xarray prots;
  46. wait_queue_head_t wq;
  47. struct workqueue_struct *work_queue;
  48. unsigned long flags;
  49. };
  50. static int pci_doe_wait(struct pci_doe_mb *doe_mb, unsigned long timeout)
  51. {
  52. if (wait_event_timeout(doe_mb->wq,
  53. test_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags),
  54. timeout))
  55. return -EIO;
  56. return 0;
  57. }
  58. static void pci_doe_write_ctrl(struct pci_doe_mb *doe_mb, u32 val)
  59. {
  60. struct pci_dev *pdev = doe_mb->pdev;
  61. int offset = doe_mb->cap_offset;
  62. pci_write_config_dword(pdev, offset + PCI_DOE_CTRL, val);
  63. }
  64. static int pci_doe_abort(struct pci_doe_mb *doe_mb)
  65. {
  66. struct pci_dev *pdev = doe_mb->pdev;
  67. int offset = doe_mb->cap_offset;
  68. unsigned long timeout_jiffies;
  69. pci_dbg(pdev, "[%x] Issuing Abort\n", offset);
  70. timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
  71. pci_doe_write_ctrl(doe_mb, PCI_DOE_CTRL_ABORT);
  72. do {
  73. int rc;
  74. u32 val;
  75. rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
  76. if (rc)
  77. return rc;
  78. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  79. /* Abort success! */
  80. if (!FIELD_GET(PCI_DOE_STATUS_ERROR, val) &&
  81. !FIELD_GET(PCI_DOE_STATUS_BUSY, val))
  82. return 0;
  83. } while (!time_after(jiffies, timeout_jiffies));
  84. /* Abort has timed out and the MB is dead */
  85. pci_err(pdev, "[%x] ABORT timed out\n", offset);
  86. return -EIO;
  87. }
  88. static int pci_doe_send_req(struct pci_doe_mb *doe_mb,
  89. struct pci_doe_task *task)
  90. {
  91. struct pci_dev *pdev = doe_mb->pdev;
  92. int offset = doe_mb->cap_offset;
  93. size_t length;
  94. u32 val;
  95. int i;
  96. /*
  97. * Check the DOE busy bit is not set. If it is set, this could indicate
  98. * someone other than Linux (e.g. firmware) is using the mailbox. Note
  99. * it is expected that firmware and OS will negotiate access rights via
  100. * an, as yet to be defined, method.
  101. */
  102. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  103. if (FIELD_GET(PCI_DOE_STATUS_BUSY, val))
  104. return -EBUSY;
  105. if (FIELD_GET(PCI_DOE_STATUS_ERROR, val))
  106. return -EIO;
  107. /* Length is 2 DW of header + length of payload in DW */
  108. length = 2 + task->request_pl_sz / sizeof(__le32);
  109. if (length > PCI_DOE_MAX_LENGTH)
  110. return -EIO;
  111. if (length == PCI_DOE_MAX_LENGTH)
  112. length = 0;
  113. /* Write DOE Header */
  114. val = FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_VID, task->prot.vid) |
  115. FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, task->prot.type);
  116. pci_write_config_dword(pdev, offset + PCI_DOE_WRITE, val);
  117. pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
  118. FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH,
  119. length));
  120. for (i = 0; i < task->request_pl_sz / sizeof(__le32); i++)
  121. pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
  122. le32_to_cpu(task->request_pl[i]));
  123. pci_doe_write_ctrl(doe_mb, PCI_DOE_CTRL_GO);
  124. return 0;
  125. }
  126. static bool pci_doe_data_obj_ready(struct pci_doe_mb *doe_mb)
  127. {
  128. struct pci_dev *pdev = doe_mb->pdev;
  129. int offset = doe_mb->cap_offset;
  130. u32 val;
  131. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  132. if (FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val))
  133. return true;
  134. return false;
  135. }
  136. static int pci_doe_recv_resp(struct pci_doe_mb *doe_mb, struct pci_doe_task *task)
  137. {
  138. struct pci_dev *pdev = doe_mb->pdev;
  139. int offset = doe_mb->cap_offset;
  140. size_t length, payload_length;
  141. u32 val;
  142. int i;
  143. /* Read the first dword to get the protocol */
  144. pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
  145. if ((FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_VID, val) != task->prot.vid) ||
  146. (FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, val) != task->prot.type)) {
  147. dev_err_ratelimited(&pdev->dev, "[%x] expected [VID, Protocol] = [%04x, %02x], got [%04x, %02x]\n",
  148. doe_mb->cap_offset, task->prot.vid, task->prot.type,
  149. FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_VID, val),
  150. FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, val));
  151. return -EIO;
  152. }
  153. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  154. /* Read the second dword to get the length */
  155. pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
  156. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  157. length = FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH, val);
  158. /* A value of 0x0 indicates max data object length */
  159. if (!length)
  160. length = PCI_DOE_MAX_LENGTH;
  161. if (length < 2)
  162. return -EIO;
  163. /* First 2 dwords have already been read */
  164. length -= 2;
  165. payload_length = min(length, task->response_pl_sz / sizeof(__le32));
  166. /* Read the rest of the response payload */
  167. for (i = 0; i < payload_length; i++) {
  168. pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
  169. task->response_pl[i] = cpu_to_le32(val);
  170. /* Prior to the last ack, ensure Data Object Ready */
  171. if (i == (payload_length - 1) && !pci_doe_data_obj_ready(doe_mb))
  172. return -EIO;
  173. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  174. }
  175. /* Flush excess length */
  176. for (; i < length; i++) {
  177. pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
  178. pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
  179. }
  180. /* Final error check to pick up on any since Data Object Ready */
  181. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  182. if (FIELD_GET(PCI_DOE_STATUS_ERROR, val))
  183. return -EIO;
  184. return min(length, task->response_pl_sz / sizeof(__le32)) * sizeof(__le32);
  185. }
  186. static void signal_task_complete(struct pci_doe_task *task, int rv)
  187. {
  188. task->rv = rv;
  189. destroy_work_on_stack(&task->work);
  190. task->complete(task);
  191. }
  192. static void signal_task_abort(struct pci_doe_task *task, int rv)
  193. {
  194. struct pci_doe_mb *doe_mb = task->doe_mb;
  195. struct pci_dev *pdev = doe_mb->pdev;
  196. if (pci_doe_abort(doe_mb)) {
  197. /*
  198. * If the device can't process an abort; set the mailbox dead
  199. * - no more submissions
  200. */
  201. pci_err(pdev, "[%x] Abort failed marking mailbox dead\n",
  202. doe_mb->cap_offset);
  203. set_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags);
  204. }
  205. signal_task_complete(task, rv);
  206. }
  207. static void doe_statemachine_work(struct work_struct *work)
  208. {
  209. struct pci_doe_task *task = container_of(work, struct pci_doe_task,
  210. work);
  211. struct pci_doe_mb *doe_mb = task->doe_mb;
  212. struct pci_dev *pdev = doe_mb->pdev;
  213. int offset = doe_mb->cap_offset;
  214. unsigned long timeout_jiffies;
  215. u32 val;
  216. int rc;
  217. if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags)) {
  218. signal_task_complete(task, -EIO);
  219. return;
  220. }
  221. /* Send request */
  222. rc = pci_doe_send_req(doe_mb, task);
  223. if (rc) {
  224. /*
  225. * The specification does not provide any guidance on how to
  226. * resolve conflicting requests from other entities.
  227. * Furthermore, it is likely that busy will not be detected
  228. * most of the time. Flag any detection of status busy with an
  229. * error.
  230. */
  231. if (rc == -EBUSY)
  232. dev_err_ratelimited(&pdev->dev, "[%x] busy detected; another entity is sending conflicting requests\n",
  233. offset);
  234. signal_task_abort(task, rc);
  235. return;
  236. }
  237. timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
  238. /* Poll for response */
  239. retry_resp:
  240. pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
  241. if (FIELD_GET(PCI_DOE_STATUS_ERROR, val)) {
  242. signal_task_abort(task, -EIO);
  243. return;
  244. }
  245. if (!FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val)) {
  246. if (time_after(jiffies, timeout_jiffies)) {
  247. signal_task_abort(task, -EIO);
  248. return;
  249. }
  250. rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
  251. if (rc) {
  252. signal_task_abort(task, rc);
  253. return;
  254. }
  255. goto retry_resp;
  256. }
  257. rc = pci_doe_recv_resp(doe_mb, task);
  258. if (rc < 0) {
  259. signal_task_abort(task, rc);
  260. return;
  261. }
  262. signal_task_complete(task, rc);
  263. }
  264. static void pci_doe_task_complete(struct pci_doe_task *task)
  265. {
  266. complete(task->private);
  267. }
  268. static int pci_doe_discovery(struct pci_doe_mb *doe_mb, u8 *index, u16 *vid,
  269. u8 *protocol)
  270. {
  271. u32 request_pl = FIELD_PREP(PCI_DOE_DATA_OBJECT_DISC_REQ_3_INDEX,
  272. *index);
  273. __le32 request_pl_le = cpu_to_le32(request_pl);
  274. __le32 response_pl_le;
  275. u32 response_pl;
  276. DECLARE_COMPLETION_ONSTACK(c);
  277. struct pci_doe_task task = {
  278. .prot.vid = PCI_VENDOR_ID_PCI_SIG,
  279. .prot.type = PCI_DOE_PROTOCOL_DISCOVERY,
  280. .request_pl = &request_pl_le,
  281. .request_pl_sz = sizeof(request_pl),
  282. .response_pl = &response_pl_le,
  283. .response_pl_sz = sizeof(response_pl),
  284. .complete = pci_doe_task_complete,
  285. .private = &c,
  286. };
  287. int rc;
  288. rc = pci_doe_submit_task(doe_mb, &task);
  289. if (rc < 0)
  290. return rc;
  291. wait_for_completion(&c);
  292. if (task.rv != sizeof(response_pl))
  293. return -EIO;
  294. response_pl = le32_to_cpu(response_pl_le);
  295. *vid = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_VID, response_pl);
  296. *protocol = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_PROTOCOL,
  297. response_pl);
  298. *index = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX,
  299. response_pl);
  300. return 0;
  301. }
  302. static void *pci_doe_xa_prot_entry(u16 vid, u8 prot)
  303. {
  304. return xa_mk_value((vid << 8) | prot);
  305. }
  306. static int pci_doe_cache_protocols(struct pci_doe_mb *doe_mb)
  307. {
  308. u8 index = 0;
  309. u8 xa_idx = 0;
  310. do {
  311. int rc;
  312. u16 vid;
  313. u8 prot;
  314. rc = pci_doe_discovery(doe_mb, &index, &vid, &prot);
  315. if (rc)
  316. return rc;
  317. pci_dbg(doe_mb->pdev,
  318. "[%x] Found protocol %d vid: %x prot: %x\n",
  319. doe_mb->cap_offset, xa_idx, vid, prot);
  320. rc = xa_insert(&doe_mb->prots, xa_idx++,
  321. pci_doe_xa_prot_entry(vid, prot), GFP_KERNEL);
  322. if (rc)
  323. return rc;
  324. } while (index);
  325. return 0;
  326. }
  327. static void pci_doe_xa_destroy(void *mb)
  328. {
  329. struct pci_doe_mb *doe_mb = mb;
  330. xa_destroy(&doe_mb->prots);
  331. }
  332. static void pci_doe_destroy_workqueue(void *mb)
  333. {
  334. struct pci_doe_mb *doe_mb = mb;
  335. destroy_workqueue(doe_mb->work_queue);
  336. }
  337. static void pci_doe_flush_mb(void *mb)
  338. {
  339. struct pci_doe_mb *doe_mb = mb;
  340. /* Stop all pending work items from starting */
  341. set_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags);
  342. /* Cancel an in progress work item, if necessary */
  343. set_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags);
  344. wake_up(&doe_mb->wq);
  345. /* Flush all work items */
  346. flush_workqueue(doe_mb->work_queue);
  347. }
  348. /**
  349. * pcim_doe_create_mb() - Create a DOE mailbox object
  350. *
  351. * @pdev: PCI device to create the DOE mailbox for
  352. * @cap_offset: Offset of the DOE mailbox
  353. *
  354. * Create a single mailbox object to manage the mailbox protocol at the
  355. * cap_offset specified.
  356. *
  357. * RETURNS: created mailbox object on success
  358. * ERR_PTR(-errno) on failure
  359. */
  360. struct pci_doe_mb *pcim_doe_create_mb(struct pci_dev *pdev, u16 cap_offset)
  361. {
  362. struct pci_doe_mb *doe_mb;
  363. struct device *dev = &pdev->dev;
  364. int rc;
  365. doe_mb = devm_kzalloc(dev, sizeof(*doe_mb), GFP_KERNEL);
  366. if (!doe_mb)
  367. return ERR_PTR(-ENOMEM);
  368. doe_mb->pdev = pdev;
  369. doe_mb->cap_offset = cap_offset;
  370. init_waitqueue_head(&doe_mb->wq);
  371. xa_init(&doe_mb->prots);
  372. rc = devm_add_action(dev, pci_doe_xa_destroy, doe_mb);
  373. if (rc)
  374. return ERR_PTR(rc);
  375. doe_mb->work_queue = alloc_ordered_workqueue("%s %s DOE [%x]", 0,
  376. dev_driver_string(&pdev->dev),
  377. pci_name(pdev),
  378. doe_mb->cap_offset);
  379. if (!doe_mb->work_queue) {
  380. pci_err(pdev, "[%x] failed to allocate work queue\n",
  381. doe_mb->cap_offset);
  382. return ERR_PTR(-ENOMEM);
  383. }
  384. rc = devm_add_action_or_reset(dev, pci_doe_destroy_workqueue, doe_mb);
  385. if (rc)
  386. return ERR_PTR(rc);
  387. /* Reset the mailbox by issuing an abort */
  388. rc = pci_doe_abort(doe_mb);
  389. if (rc) {
  390. pci_err(pdev, "[%x] failed to reset mailbox with abort command : %d\n",
  391. doe_mb->cap_offset, rc);
  392. return ERR_PTR(rc);
  393. }
  394. /*
  395. * The state machine and the mailbox should be in sync now;
  396. * Set up mailbox flush prior to using the mailbox to query protocols.
  397. */
  398. rc = devm_add_action_or_reset(dev, pci_doe_flush_mb, doe_mb);
  399. if (rc)
  400. return ERR_PTR(rc);
  401. rc = pci_doe_cache_protocols(doe_mb);
  402. if (rc) {
  403. pci_err(pdev, "[%x] failed to cache protocols : %d\n",
  404. doe_mb->cap_offset, rc);
  405. return ERR_PTR(rc);
  406. }
  407. return doe_mb;
  408. }
  409. EXPORT_SYMBOL_GPL(pcim_doe_create_mb);
  410. /**
  411. * pci_doe_supports_prot() - Return if the DOE instance supports the given
  412. * protocol
  413. * @doe_mb: DOE mailbox capability to query
  414. * @vid: Protocol Vendor ID
  415. * @type: Protocol type
  416. *
  417. * RETURNS: True if the DOE mailbox supports the protocol specified
  418. */
  419. bool pci_doe_supports_prot(struct pci_doe_mb *doe_mb, u16 vid, u8 type)
  420. {
  421. unsigned long index;
  422. void *entry;
  423. /* The discovery protocol must always be supported */
  424. if (vid == PCI_VENDOR_ID_PCI_SIG && type == PCI_DOE_PROTOCOL_DISCOVERY)
  425. return true;
  426. xa_for_each(&doe_mb->prots, index, entry)
  427. if (entry == pci_doe_xa_prot_entry(vid, type))
  428. return true;
  429. return false;
  430. }
  431. EXPORT_SYMBOL_GPL(pci_doe_supports_prot);
  432. /**
  433. * pci_doe_submit_task() - Submit a task to be processed by the state machine
  434. *
  435. * @doe_mb: DOE mailbox capability to submit to
  436. * @task: task to be queued
  437. *
  438. * Submit a DOE task (request/response) to the DOE mailbox to be processed.
  439. * Returns upon queueing the task object. If the queue is full this function
  440. * will sleep until there is room in the queue.
  441. *
  442. * task->complete will be called when the state machine is done processing this
  443. * task.
  444. *
  445. * @task must be allocated on the stack.
  446. *
  447. * Excess data will be discarded.
  448. *
  449. * RETURNS: 0 when task has been successfully queued, -ERRNO on error
  450. */
  451. int pci_doe_submit_task(struct pci_doe_mb *doe_mb, struct pci_doe_task *task)
  452. {
  453. if (!pci_doe_supports_prot(doe_mb, task->prot.vid, task->prot.type))
  454. return -EINVAL;
  455. /*
  456. * DOE requests must be a whole number of DW and the response needs to
  457. * be big enough for at least 1 DW
  458. */
  459. if (task->request_pl_sz % sizeof(__le32) ||
  460. task->response_pl_sz < sizeof(__le32))
  461. return -EINVAL;
  462. if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags))
  463. return -EIO;
  464. task->doe_mb = doe_mb;
  465. INIT_WORK_ONSTACK(&task->work, doe_statemachine_work);
  466. queue_work(doe_mb->work_queue, &task->work);
  467. return 0;
  468. }
  469. EXPORT_SYMBOL_GPL(pci_doe_submit_task);