1 // SPDX-License-Identifier: GPL-2.0
4 * PCIe r6.0, sec 6.30 DOE
6 * Copyright (C) 2021 Huawei
7 * Jonathan Cameron <Jonathan.Cameron@huawei.com>
9 * Copyright (C) 2022 Intel Corporation
10 * Ira Weiny <ira.weiny@intel.com>
13 #define dev_fmt(fmt) "DOE: " fmt
15 #include <linux/bitfield.h>
16 #include <linux/delay.h>
17 #include <linux/jiffies.h>
18 #include <linux/mutex.h>
19 #include <linux/pci.h>
20 #include <linux/pci-doe.h>
21 #include <linux/workqueue.h>
23 #define PCI_DOE_PROTOCOL_DISCOVERY 0
25 /* Timeout of 1 second from 6.30.2 Operation, PCI Spec r6.0 */
26 #define PCI_DOE_TIMEOUT HZ
27 #define PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128)
29 #define PCI_DOE_FLAG_CANCEL 0
30 #define PCI_DOE_FLAG_DEAD 1
33 * struct pci_doe_mb - State for a single DOE mailbox
35 * This state is used to manage a single DOE mailbox capability. All fields
36 * should be considered opaque to the consumers and the structure passed into
37 * the helpers below after being created by devm_pci_doe_create()
39 * @pdev: PCI device this mailbox belongs to
40 * @cap_offset: Capability offset
41 * @prots: Array of protocols supported (encoded as long values)
42 * @wq: Wait queue for work item
43 * @work_queue: Queue of pci_doe_work items
44 * @flags: Bit array of PCI_DOE_FLAG_* flags
52 struct workqueue_struct *work_queue;
56 static int pci_doe_wait(struct pci_doe_mb *doe_mb, unsigned long timeout)
58 if (wait_event_timeout(doe_mb->wq,
59 test_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags),
65 static void pci_doe_write_ctrl(struct pci_doe_mb *doe_mb, u32 val)
67 struct pci_dev *pdev = doe_mb->pdev;
68 int offset = doe_mb->cap_offset;
70 pci_write_config_dword(pdev, offset + PCI_DOE_CTRL, val);
73 static int pci_doe_abort(struct pci_doe_mb *doe_mb)
75 struct pci_dev *pdev = doe_mb->pdev;
76 int offset = doe_mb->cap_offset;
77 unsigned long timeout_jiffies;
79 pci_dbg(pdev, "[%x] Issuing Abort\n", offset);
81 timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
82 pci_doe_write_ctrl(doe_mb, PCI_DOE_CTRL_ABORT);
88 rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
91 pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
94 if (!FIELD_GET(PCI_DOE_STATUS_ERROR, val) &&
95 !FIELD_GET(PCI_DOE_STATUS_BUSY, val))
98 } while (!time_after(jiffies, timeout_jiffies));
100 /* Abort has timed out and the MB is dead */
101 pci_err(pdev, "[%x] ABORT timed out\n", offset);
105 static int pci_doe_send_req(struct pci_doe_mb *doe_mb,
106 struct pci_doe_task *task)
108 struct pci_dev *pdev = doe_mb->pdev;
109 int offset = doe_mb->cap_offset;
114 * Check the DOE busy bit is not set. If it is set, this could indicate
115 * someone other than Linux (e.g. firmware) is using the mailbox. Note
116 * it is expected that firmware and OS will negotiate access rights via
117 * an, as yet to be defined, method.
119 pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
120 if (FIELD_GET(PCI_DOE_STATUS_BUSY, val))
123 if (FIELD_GET(PCI_DOE_STATUS_ERROR, val))
126 /* Write DOE Header */
127 val = FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_VID, task->prot.vid) |
128 FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, task->prot.type);
129 pci_write_config_dword(pdev, offset + PCI_DOE_WRITE, val);
130 /* Length is 2 DW of header + length of payload in DW */
131 pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
132 FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH,
133 2 + task->request_pl_sz /
135 for (i = 0; i < task->request_pl_sz / sizeof(u32); i++)
136 pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
137 task->request_pl[i]);
139 pci_doe_write_ctrl(doe_mb, PCI_DOE_CTRL_GO);
144 static bool pci_doe_data_obj_ready(struct pci_doe_mb *doe_mb)
146 struct pci_dev *pdev = doe_mb->pdev;
147 int offset = doe_mb->cap_offset;
150 pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
151 if (FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val))
156 static int pci_doe_recv_resp(struct pci_doe_mb *doe_mb, struct pci_doe_task *task)
158 struct pci_dev *pdev = doe_mb->pdev;
159 int offset = doe_mb->cap_offset;
160 size_t length, payload_length;
164 /* Read the first dword to get the protocol */
165 pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
166 if ((FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_VID, val) != task->prot.vid) ||
167 (FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, val) != task->prot.type)) {
168 dev_err_ratelimited(&pdev->dev, "[%x] expected [VID, Protocol] = [%04x, %02x], got [%04x, %02x]\n",
169 doe_mb->cap_offset, task->prot.vid, task->prot.type,
170 FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_VID, val),
171 FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, val));
175 pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
176 /* Read the second dword to get the length */
177 pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
178 pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
180 length = FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH, val);
181 if (length > SZ_1M || length < 2)
184 /* First 2 dwords have already been read */
186 payload_length = min(length, task->response_pl_sz / sizeof(u32));
187 /* Read the rest of the response payload */
188 for (i = 0; i < payload_length; i++) {
189 pci_read_config_dword(pdev, offset + PCI_DOE_READ,
190 &task->response_pl[i]);
191 /* Prior to the last ack, ensure Data Object Ready */
192 if (i == (payload_length - 1) && !pci_doe_data_obj_ready(doe_mb))
194 pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
197 /* Flush excess length */
198 for (; i < length; i++) {
199 pci_read_config_dword(pdev, offset + PCI_DOE_READ, &val);
200 pci_write_config_dword(pdev, offset + PCI_DOE_READ, 0);
203 /* Final error check to pick up on any since Data Object Ready */
204 pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
205 if (FIELD_GET(PCI_DOE_STATUS_ERROR, val))
208 return min(length, task->response_pl_sz / sizeof(u32)) * sizeof(u32);
211 static void signal_task_complete(struct pci_doe_task *task, int rv)
214 task->complete(task);
217 static void signal_task_abort(struct pci_doe_task *task, int rv)
219 struct pci_doe_mb *doe_mb = task->doe_mb;
220 struct pci_dev *pdev = doe_mb->pdev;
222 if (pci_doe_abort(doe_mb)) {
224 * If the device can't process an abort; set the mailbox dead
225 * - no more submissions
227 pci_err(pdev, "[%x] Abort failed marking mailbox dead\n",
229 set_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags);
231 signal_task_complete(task, rv);
234 static void doe_statemachine_work(struct work_struct *work)
236 struct pci_doe_task *task = container_of(work, struct pci_doe_task,
238 struct pci_doe_mb *doe_mb = task->doe_mb;
239 struct pci_dev *pdev = doe_mb->pdev;
240 int offset = doe_mb->cap_offset;
241 unsigned long timeout_jiffies;
245 if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags)) {
246 signal_task_complete(task, -EIO);
251 rc = pci_doe_send_req(doe_mb, task);
254 * The specification does not provide any guidance on how to
255 * resolve conflicting requests from other entities.
256 * Furthermore, it is likely that busy will not be detected
257 * most of the time. Flag any detection of status busy with an
261 dev_err_ratelimited(&pdev->dev, "[%x] busy detected; another entity is sending conflicting requests\n",
263 signal_task_abort(task, rc);
267 timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
268 /* Poll for response */
270 pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
271 if (FIELD_GET(PCI_DOE_STATUS_ERROR, val)) {
272 signal_task_abort(task, -EIO);
276 if (!FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val)) {
277 if (time_after(jiffies, timeout_jiffies)) {
278 signal_task_abort(task, -EIO);
281 rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
283 signal_task_abort(task, rc);
289 rc = pci_doe_recv_resp(doe_mb, task);
291 signal_task_abort(task, rc);
295 signal_task_complete(task, rc);
298 static void pci_doe_task_complete(struct pci_doe_task *task)
300 complete(task->private);
303 static int pci_doe_discovery(struct pci_doe_mb *doe_mb, u8 *index, u16 *vid,
306 u32 request_pl = FIELD_PREP(PCI_DOE_DATA_OBJECT_DISC_REQ_3_INDEX,
309 DECLARE_COMPLETION_ONSTACK(c);
310 struct pci_doe_task task = {
311 .prot.vid = PCI_VENDOR_ID_PCI_SIG,
312 .prot.type = PCI_DOE_PROTOCOL_DISCOVERY,
313 .request_pl = &request_pl,
314 .request_pl_sz = sizeof(request_pl),
315 .response_pl = &response_pl,
316 .response_pl_sz = sizeof(response_pl),
317 .complete = pci_doe_task_complete,
322 rc = pci_doe_submit_task(doe_mb, &task);
326 wait_for_completion(&c);
328 if (task.rv != sizeof(response_pl))
331 *vid = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_VID, response_pl);
332 *protocol = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_PROTOCOL,
334 *index = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX,
340 static void *pci_doe_xa_prot_entry(u16 vid, u8 prot)
342 return xa_mk_value((vid << 8) | prot);
345 static int pci_doe_cache_protocols(struct pci_doe_mb *doe_mb)
355 rc = pci_doe_discovery(doe_mb, &index, &vid, &prot);
359 pci_dbg(doe_mb->pdev,
360 "[%x] Found protocol %d vid: %x prot: %x\n",
361 doe_mb->cap_offset, xa_idx, vid, prot);
363 rc = xa_insert(&doe_mb->prots, xa_idx++,
364 pci_doe_xa_prot_entry(vid, prot), GFP_KERNEL);
372 static void pci_doe_xa_destroy(void *mb)
374 struct pci_doe_mb *doe_mb = mb;
376 xa_destroy(&doe_mb->prots);
379 static void pci_doe_destroy_workqueue(void *mb)
381 struct pci_doe_mb *doe_mb = mb;
383 destroy_workqueue(doe_mb->work_queue);
386 static void pci_doe_flush_mb(void *mb)
388 struct pci_doe_mb *doe_mb = mb;
390 /* Stop all pending work items from starting */
391 set_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags);
393 /* Cancel an in progress work item, if necessary */
394 set_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags);
395 wake_up(&doe_mb->wq);
397 /* Flush all work items */
398 flush_workqueue(doe_mb->work_queue);
402 * pcim_doe_create_mb() - Create a DOE mailbox object
404 * @pdev: PCI device to create the DOE mailbox for
405 * @cap_offset: Offset of the DOE mailbox
407 * Create a single mailbox object to manage the mailbox protocol at the
408 * cap_offset specified.
410 * RETURNS: created mailbox object on success
411 * ERR_PTR(-errno) on failure
413 struct pci_doe_mb *pcim_doe_create_mb(struct pci_dev *pdev, u16 cap_offset)
415 struct pci_doe_mb *doe_mb;
416 struct device *dev = &pdev->dev;
419 doe_mb = devm_kzalloc(dev, sizeof(*doe_mb), GFP_KERNEL);
421 return ERR_PTR(-ENOMEM);
424 doe_mb->cap_offset = cap_offset;
425 init_waitqueue_head(&doe_mb->wq);
427 xa_init(&doe_mb->prots);
428 rc = devm_add_action(dev, pci_doe_xa_destroy, doe_mb);
432 doe_mb->work_queue = alloc_ordered_workqueue("%s %s DOE [%x]", 0,
433 dev_driver_string(&pdev->dev),
436 if (!doe_mb->work_queue) {
437 pci_err(pdev, "[%x] failed to allocate work queue\n",
439 return ERR_PTR(-ENOMEM);
441 rc = devm_add_action_or_reset(dev, pci_doe_destroy_workqueue, doe_mb);
445 /* Reset the mailbox by issuing an abort */
446 rc = pci_doe_abort(doe_mb);
448 pci_err(pdev, "[%x] failed to reset mailbox with abort command : %d\n",
449 doe_mb->cap_offset, rc);
454 * The state machine and the mailbox should be in sync now;
455 * Set up mailbox flush prior to using the mailbox to query protocols.
457 rc = devm_add_action_or_reset(dev, pci_doe_flush_mb, doe_mb);
461 rc = pci_doe_cache_protocols(doe_mb);
463 pci_err(pdev, "[%x] failed to cache protocols : %d\n",
464 doe_mb->cap_offset, rc);
470 EXPORT_SYMBOL_GPL(pcim_doe_create_mb);
473 * pci_doe_supports_prot() - Return if the DOE instance supports the given
475 * @doe_mb: DOE mailbox capability to query
476 * @vid: Protocol Vendor ID
477 * @type: Protocol type
479 * RETURNS: True if the DOE mailbox supports the protocol specified
481 bool pci_doe_supports_prot(struct pci_doe_mb *doe_mb, u16 vid, u8 type)
486 /* The discovery protocol must always be supported */
487 if (vid == PCI_VENDOR_ID_PCI_SIG && type == PCI_DOE_PROTOCOL_DISCOVERY)
490 xa_for_each(&doe_mb->prots, index, entry)
491 if (entry == pci_doe_xa_prot_entry(vid, type))
496 EXPORT_SYMBOL_GPL(pci_doe_supports_prot);
499 * pci_doe_submit_task() - Submit a task to be processed by the state machine
501 * @doe_mb: DOE mailbox capability to submit to
502 * @task: task to be queued
504 * Submit a DOE task (request/response) to the DOE mailbox to be processed.
505 * Returns upon queueing the task object. If the queue is full this function
506 * will sleep until there is room in the queue.
508 * task->complete will be called when the state machine is done processing this
511 * Excess data will be discarded.
513 * RETURNS: 0 when task has been successfully queued, -ERRNO on error
515 int pci_doe_submit_task(struct pci_doe_mb *doe_mb, struct pci_doe_task *task)
517 if (!pci_doe_supports_prot(doe_mb, task->prot.vid, task->prot.type))
521 * DOE requests must be a whole number of DW and the response needs to
522 * be big enough for at least 1 DW
524 if (task->request_pl_sz % sizeof(u32) ||
525 task->response_pl_sz < sizeof(u32))
528 if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags))
531 task->doe_mb = doe_mb;
532 INIT_WORK(&task->work, doe_statemachine_work);
533 queue_work(doe_mb->work_queue, &task->work);
536 EXPORT_SYMBOL_GPL(pci_doe_submit_task);