[no subject]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 
> 
> > +               doe->state = DOE_WAIT_ABORT;
> > +               pci_doe_abort_start(doe);
> > +               return;
> > +       }
> > +
> > +       switch (doe->state) {
> > +       case DOE_IDLE:
> > +               if (task == NULL)
> > +                       return;
> > +
> > +               /* Nothing currently in flight so queue a task */
> > +               rc = pci_doe_send_req(doe, task->ex);
> > +               /*
> > +                * The specification does not provide any guidance on how long
> > +                * some other entity could keep the DOE busy, so try for 1
> > +                * second then fail. Busy handling is best effort only, because
> > +                * there is no way of avoiding racing against another user of
> > +                * the DOE.
> > +                */
> > +               if (rc == -EBUSY) {
> > +                       doe->busy_retries++;
> > +                       if (doe->busy_retries == PCI_DOE_BUSY_MAX_RETRIES) {
> > +                               /* Long enough, fail this request */
> > +                               pci_warn(pdev,
> > +                                       "DOE busy for too long (> 1 sec)\n");
> > +                               doe->busy_retries = 0;
> > +                               goto err_busy;
> > +                       }
> > +                       schedule_delayed_work(w, HZ / PCI_DOE_BUSY_MAX_RETRIES);
> > +                       return;
> > +               }
> > +               if (rc)
> > +                       goto err_abort;
> > +               doe->busy_retries = 0;
> > +
> > +               doe->state = DOE_WAIT_RESP;
> > +               doe->timeout_jiffies = jiffies + HZ;
> > +               /* Now poll or wait for IRQ with timeout */
> > +               if (doe->irq > 0)
> > +                       schedule_delayed_work(w, PCI_DOE_TIMEOUT);
> > +               else
> > +                       schedule_delayed_work(w, PCI_DOE_POLL_INTERVAL);
> > +               return;
> > +
> > +       case DOE_WAIT_RESP:
> > +               /* Not possible to get here with NULL task */
> > +               pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
> > +               if (FIELD_GET(PCI_DOE_STATUS_ERROR, val)) {
> > +                       rc = -EIO;
> > +                       goto err_abort;
> > +               }
> > +
> > +               if (!FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val)) {
> > +                       /* If not yet at timeout reschedule otherwise abort */
> > +                       if (time_after(jiffies, doe->timeout_jiffies)) {
> > +                               rc = -ETIMEDOUT;
> > +                               goto err_abort;
> > +                       }
> > +                       schedule_delayed_work(w, PCI_DOE_POLL_INTERVAL);
> > +                       return;
> > +               }
> > +
> > +               rc  = pci_doe_recv_resp(doe, task->ex);
> > +               if (rc < 0)
> > +                       goto err_abort;
> > +
> > +               doe->state = DOE_IDLE;
> > +
> > +               mutex_lock(&doe->state_lock);
> > +               doe->cur_task = NULL;
> > +               mutex_unlock(&doe->state_lock);
> > +               wake_up_interruptible(&doe->wq);
> > +
> > +               /* Set the return value to the length of received payload */
> > +               task->rv = rc;
> > +               task->cb(task->private);
> > +
> > +               return;
> > +
> > +       case DOE_WAIT_ABORT:
> > +       case DOE_WAIT_ABORT_ON_ERR:
> > +               pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
> > +
> > +               if (!FIELD_GET(PCI_DOE_STATUS_ERROR, val) &&
> > +                   !FIELD_GET(PCI_DOE_STATUS_BUSY, val)) {
> > +                       /* Back to normal state - carry on */
> > +                       mutex_lock(&doe->state_lock);
> > +                       doe->cur_task = NULL;
> > +                       mutex_unlock(&doe->state_lock);
> > +                       wake_up_interruptible(&doe->wq);
> > +
> > +                       /*
> > +                        * For deliberately triggered abort, someone is
> > +                        * waiting.
> > +                        */
> > +                       if (doe->state == DOE_WAIT_ABORT)
> > +                               complete(&doe->abort_c);  
> 
> Why is a completion and waitqueue needed? I.e. a waiter could simply
> look for an abort completion flag to be set instead.

You mean use the main completion (the one for the non abort case)
and a flag? 

Or a wait_event() with appropriate check?

Could do that but I'm not sure I understand why we care either way?

> 
> 
> > +
> > +                       doe->state = DOE_IDLE;
> > +                       return;
> > +               }
> > +               if (time_after(jiffies, doe->timeout_jiffies)) {
> > +                       /* Task has timed out and is dead - abort */
> > +                       pci_err(pdev, "DOE ABORT timed out\n");
> > +                       mutex_lock(&doe->state_lock);
> > +                       doe->dead = true;
> > +                       doe->cur_task = NULL;
> > +                       mutex_unlock(&doe->state_lock);
> > +                       wake_up_interruptible(&doe->wq);
> > +
> > +                       if (doe->state == DOE_WAIT_ABORT)
> > +                               complete(&doe->abort_c);
> > +               }
> > +               return;
> > +       }
> > +
> > +err_abort:
> > +       doe->state = DOE_WAIT_ABORT_ON_ERR;
> > +       pci_doe_abort_start(doe);
> > +err_busy:
> > +       task->rv = rc;
> > +       task->cb(task->private);
> > +       /* If here via err_busy, signal the task done. */
> > +       if (doe->state == DOE_IDLE) {
> > +               mutex_lock(&doe->state_lock);
> > +               doe->cur_task = NULL;
> > +               mutex_unlock(&doe->state_lock);
> > +               wake_up_interruptible(&doe->wq);
> > +       }
> > +}
> > +
> > +static void pci_doe_task_complete(void *private)
> > +{
> > +       complete(private);
> > +}
> > +
> > +/**
> > + * pci_doe_exchange_sync() - Send a request, then wait for and receive a
> > + *                          response
> > + * @doe_dev: DOE mailbox state structure
> > + * @ex: Description of the buffers and Vendor ID + type used in this
> > + *      request/response pair
> > + *
> > + * Excess data will be discarded.
> > + *
> > + * RETURNS: payload in bytes on success, < 0 on error
> > + */
> > +int pci_doe_exchange_sync(struct pci_doe_dev *doe_dev,
> > +                         struct pci_doe_exchange *ex)
> > +{
> > +       struct pci_doe *doe = dev_get_drvdata(&doe_dev->adev.dev);
> > +       struct pci_doe_task task;
> > +       DECLARE_COMPLETION_ONSTACK(c);
> > +
> > +       if (!doe)
> > +               return -EAGAIN;
> > +
> > +       /* DOE requests must be a whole number of DW */
> > +       if (ex->request_pl_sz % sizeof(u32))
> > +               return -EINVAL;
> > +
> > +       task.ex = ex;
> > +       task.cb = pci_doe_task_complete;
> > +       task.private = &c;
> > +
> > +again:
> > +       mutex_lock(&doe->state_lock);
> > +       if (doe->cur_task) {
> > +               mutex_unlock(&doe->state_lock);
> > +               wait_event_interruptible(doe->wq, doe->cur_task == NULL);
> > +               goto again;
> > +       }
> > +
> > +       if (doe->dead) {
> > +               mutex_unlock(&doe->state_lock);
> > +               return -EIO;
> > +       }
> > +       doe->cur_task = &task;
> > +       schedule_delayed_work(&doe->statemachine, 0);
> > +       mutex_unlock(&doe->state_lock);
> > +
> > +       wait_for_completion(&c);  
> 
> I would expect that the caller of this routine would want to specify
> the task and end_task() callback and use that as the completion
> signal. It may also want "no wait" behavior where it is prepared for
> the DOE result to come back sometime later. With that change the
> exchange fields can move into the task directly.

This is the simple synchronous wrapper around an async core.
If we want an async path at somepoint in the future where we have
someone using it then sure, we can have an async version that
takes the callback.

> 
> > +
> > +       return task.rv;
> > +}
> > +EXPORT_SYMBOL_GPL(pci_doe_exchange_sync);
> > +
> > +/**
> > + * pci_doe_supports_prot() - Return if the DOE instance supports the given
> > + *                          protocol
> > + * @pdev: Device on which to find the DOE instance
> > + * @vid: Protocol Vendor ID
> > + * @type: protocol type
> > + *
> > + * This device can then be passed to pci_doe_exchange_sync() to execute a
> > + * mailbox exchange through that DOE mailbox.
> > + *
> > + * RETURNS: True if the DOE device supports the protocol specified
> > + */
> > +bool pci_doe_supports_prot(struct pci_doe_dev *doe_dev, u16 vid, u8 type)
> > +{
> > +       struct pci_doe *doe = dev_get_drvdata(&doe_dev->adev.dev);
> > +       int i;
> > +
> > +       if (!doe)
> > +               return false;
> > +
> > +       for (i = 0; i < doe->num_prots; i++)
> > +               if ((doe->prots[i].vid == vid) &&
> > +                   (doe->prots[i].type == type))
> > +                       return true;
> > +
> > +       return false;
> > +}
> > +EXPORT_SYMBOL_GPL(pci_doe_supports_prot);
> > +
> > +static int pci_doe_discovery(struct pci_doe *doe, u8 *index, u16 *vid,
> > +                            u8 *protocol)
> > +{
> > +       u32 request_pl = FIELD_PREP(PCI_DOE_DATA_OBJECT_DISC_REQ_3_INDEX,
> > +                                   *index);
> > +       u32 response_pl;
> > +       struct pci_doe_exchange ex = {
> > +               .prot.vid = PCI_VENDOR_ID_PCI_SIG,
> > +               .prot.type = PCI_DOE_PROTOCOL_DISCOVERY,
> > +               .request_pl = &request_pl,
> > +               .request_pl_sz = sizeof(request_pl),
> > +               .response_pl = &response_pl,
> > +               .response_pl_sz = sizeof(response_pl),
> > +       };
> > +       int ret;
> > +
> > +       ret = pci_doe_exchange_sync(doe->doe_dev, &ex);
> > +       if (ret < 0)
> > +               return ret;
> > +
> > +       if (ret != sizeof(response_pl))
> > +               return -EIO;
> > +
> > +       *vid = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_VID, response_pl);
> > +       *protocol = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_PROTOCOL,
> > +                             response_pl);
> > +       *index = FIELD_GET(PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX,
> > +                          response_pl);
> > +
> > +       return 0;
> > +}
> > +
> > +static int pci_doe_cache_protocols(struct pci_doe *doe)
> > +{
> > +       u8 index = 0;
> > +       int num_prots;
> > +       int rc;
> > +
> > +       /* Discovery protocol must always be supported and must report itself */
> > +       num_prots = 1;
> > +       doe->prots = devm_kcalloc(&doe->doe_dev->adev.dev, num_prots,
> > +                                 sizeof(*doe->prots), GFP_KERNEL);
> > +       if (doe->prots == NULL)
> > +               return -ENOMEM;
> > +
> > +       do {
> > +               struct pci_doe_protocol *prot;
> > +
> > +               prot = &doe->prots[num_prots - 1];
> > +               rc = pci_doe_discovery(doe, &index, &prot->vid, &prot->type);
> > +               if (rc)
> > +                       return rc;
> > +
> > +               if (index) {
> > +                       struct pci_doe_protocol *prot_new;
> > +
> > +                       num_prots++;
> > +                       prot_new = devm_krealloc(&doe->doe_dev->adev.dev,
> > +                                                doe->prots,
> > +                                                sizeof(*doe->prots) *
> > +                                                       num_prots,
> > +                                                GFP_KERNEL);
> > +                       if (prot_new == NULL)
> > +                               return -ENOMEM;
> > +                       doe->prots = prot_new;
> > +               }
> > +       } while (index);
> > +
> > +       doe->num_prots = num_prots;
> > +       return 0;
> > +}
> > +
> > +static int pci_doe_abort(struct pci_doe *doe)
> > +{
> > +       reinit_completion(&doe->abort_c);
> > +       mutex_lock(&doe->state_lock);
> > +       doe->abort = true;  
> 
> Why not a flags field where atomic bitops can be used without need for a mutex.

I'll go the other way, why bother with atomics when this isn't a high performance
path or something expected to happen often?

> 
> > +       mutex_unlock(&doe->state_lock);
> > +       schedule_delayed_work(&doe->statemachine, 0);
> > +       wait_for_completion(&doe->abort_c);
> > +
> > +       if (doe->dead)  
> 
> dead could also be another atomic flag.

Probably true, but I'm really not getting why that would be a beneficial
thing to do.

> 
> > +               return -EIO;
> > +
> > +       return 0;
> > +}
> > +
> > +static int pci_doe_reg_irq(struct pci_doe *doe)
> > +{
> > +       struct pci_dev *pdev = doe->doe_dev->pdev;
> > +       bool poll = !pci_dev_msi_enabled(pdev);
> > +       int offset = doe->doe_dev->cap_offset;
> > +       int rc, irq;
> > +       u32 val;
> > +
> > +       pci_read_config_dword(pdev, offset + PCI_DOE_CAP, &val);
> > +
> > +       if (!poll && FIELD_GET(PCI_DOE_CAP_INT, val)) {
> > +               irq = pci_irq_vector(pdev, FIELD_GET(PCI_DOE_CAP_IRQ, val));
> > +               if (irq < 0)
> > +                       return irq;
> > +
> > +               doe->irq_name = devm_kasprintf(&doe->doe_dev->adev.dev,
> > +                                               GFP_KERNEL,
> > +                                               "DOE[%s]",
> > +                                               doe->doe_dev->adev.name);
> > +               if (!doe->irq_name)
> > +                       return -ENOMEM;
> > +
> > +               rc = devm_request_irq(&pdev->dev, irq, pci_doe_irq, 0,
> > +                                     doe->irq_name, doe);
> > +               if (rc)
> > +                       return rc;
> > +
> > +               doe->irq = irq;
> > +               pci_write_config_dword(pdev, offset + PCI_DOE_CTRL,
> > +                                      PCI_DOE_CTRL_INT_EN);
> > +       }
> > +
> > +       return 0;
> > +}
> > +
> > +/*
> > + * pci_doe_probe() - Set up the Mailbox
> > + * @aux_dev: Auxiliary Device
> > + * @id: Auxiliary device ID
> > + *
> > + * Probe the mailbox found for all protocols and set up the Mailbox
> > + *
> > + * RETURNS: 0 on success, < 0 on error
> > + */
> > +static int pci_doe_probe(struct auxiliary_device *aux_dev,
> > +                        const struct auxiliary_device_id *id)
> > +{
> > +       struct pci_doe_dev *doe_dev = container_of(aux_dev,
> > +                                       struct pci_doe_dev,
> > +                                       adev);
> > +       struct pci_doe *doe;
> > +       int rc;
> > +
> > +       doe = devm_kzalloc(&aux_dev->dev, sizeof(*doe), GFP_KERNEL);
> > +       if (!doe)
> > +               return -ENOMEM;
> > +
> > +       mutex_init(&doe->state_lock);
> > +       init_completion(&doe->abort_c);
> > +       doe->doe_dev = doe_dev;
> > +       init_waitqueue_head(&doe->wq);
> > +       INIT_DELAYED_WORK(&doe->statemachine, doe_statemachine_work);
> > +       dev_set_drvdata(&aux_dev->dev, doe);
> > +
> > +       rc = pci_doe_reg_irq(doe);
> > +       if (rc)
> > +               return rc;
> > +
> > +       /* Reset the mailbox by issuing an abort */
> > +       rc = pci_doe_abort(doe);
> > +       if (rc)
> > +               return rc;
> > +
> > +       rc = pci_doe_cache_protocols(doe);
> > +       if (rc)
> > +               return rc;  
> 
> This can just be:
> 
>  return pci_doe_cache_protocols(doe);
> 
> > +
> > +       return 0;
> > +}
> > +
> > +static void pci_doe_remove(struct auxiliary_device *aux_dev)
> > +{
> > +       struct pci_doe *doe = dev_get_drvdata(&aux_dev->dev);
> > +
> > +       /* First halt the state machine */
> > +       cancel_delayed_work_sync(&doe->statemachine);
> > +}
> > +
> > +static const struct auxiliary_device_id pci_doe_auxiliary_id_table[] = {
> > +       {},
> > +};
> > +
> > +MODULE_DEVICE_TABLE(auxiliary, pci_doe_auxiliary_id_table);  
> 
> Why is this empty table here?
> 
> > +
> > +struct auxiliary_driver pci_doe_auxiliary_drv = {
> > +       .name = "pci_doe",
> > +       .id_table = pci_doe_auxiliary_id_table,
> > +       .probe = pci_doe_probe,
> > +       .remove = pci_doe_remove
> > +};  
> 
> I expect that these helpers would be provided by the PCI core, but
> then a subsystem like CXL would have code to register their auxiliary
> devices and drivers that mostly just wrap the PCI core DOE
> implementation.
> 
> > +
> > +static int __init pci_doe_init_module(void)
> > +{
> > +       int ret;
> > +
> > +       ret = auxiliary_driver_register(&pci_doe_auxiliary_drv);
> > +       if (ret) {
> > +               pr_err("Failed pci_doe auxiliary_driver_register() ret=%d\n",
> > +                      ret);
> > +               return ret;
> > +       }
> > +
> > +       return 0;
> > +}
> > +
> > +static void __exit pci_doe_exit_module(void)
> > +{
> > +       auxiliary_driver_unregister(&pci_doe_auxiliary_drv);
> > +}
> > +
> > +module_init(pci_doe_init_module);
> > +module_exit(pci_doe_exit_module);
> > +MODULE_LICENSE("GPL v2");
> > diff --git a/include/linux/pci-doe.h b/include/linux/pci-doe.h
> > new file mode 100644
> > index 000000000000..2f52b31c6f32
> > --- /dev/null
> > +++ b/include/linux/pci-doe.h
> > @@ -0,0 +1,60 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +/*
> > + * Data Object Exchange was added as an ECN to the PCIe r5.0 spec.
> > + *
> > + * Copyright (C) 2021 Huawei
> > + *     Jonathan Cameron <Jonathan.Cameron@xxxxxxxxxx>
> > + */
> > +
> > +#include <linux/completion.h>
> > +#include <linux/list.h>
> > +#include <linux/auxiliary_bus.h>
> > +
> > +#ifndef LINUX_PCI_DOE_H
> > +#define LINUX_PCI_DOE_H
> > +
> > +struct pci_doe_protocol {
> > +       u16 vid;
> > +       u8 type;
> > +};
> > +
> > +/**
> > + * struct pci_doe_exchange - represents a single query/response
> > + *
> > + * @prot: DOE Protocol
> > + * @request_pl: The request payload
> > + * @request_pl_sz: Size of the request payload
> > + * @response_pl: The response payload
> > + * @response_pl_sz: Size of the response payload
> > + */
> > +struct pci_doe_exchange {
> > +       struct pci_doe_protocol prot;
> > +       u32 *request_pl;
> > +       size_t request_pl_sz;
> > +       u32 *response_pl;
> > +       size_t response_pl_sz;
> > +};
> > +
> > +/**
> > + * struct pci_doe_dev - DOE mailbox device
> > + *
> > + * @adrv: Auxiliary Driver data
> > + * @pdev: PCI device this belongs to
> > + * @offset: Capability offset
> > + *
> > + * This represents a single DOE mailbox device.  Devices should create this
> > + * device and register it on the Auxiliary bus for the DOE driver to maintain.
> > + *
> > + */
> > +struct pci_doe_dev {
> > +       struct auxiliary_device adev;
> > +       struct pci_dev *pdev;
> > +       int cap_offset;
> > +};
> > +
> > +/* Library operations */
> > +int pci_doe_exchange_sync(struct pci_doe_dev *doe_dev,
> > +                                struct pci_doe_exchange *ex);
> > +bool pci_doe_supports_prot(struct pci_doe_dev *doe_dev, u16 vid, u8 type);
> > +
> > +#endif
> > diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h
> > index ff6ccbc6efe9..c04aad391669 100644
> > --- a/include/uapi/linux/pci_regs.h
> > +++ b/include/uapi/linux/pci_regs.h
> > @@ -736,7 +736,8 @@
> >  #define PCI_EXT_CAP_ID_DVSEC   0x23    /* Designated Vendor-Specific */
> >  #define PCI_EXT_CAP_ID_DLF     0x25    /* Data Link Feature */
> >  #define PCI_EXT_CAP_ID_PL_16GT 0x26    /* Physical Layer 16.0 GT/s */
> > -#define PCI_EXT_CAP_ID_MAX     PCI_EXT_CAP_ID_PL_16GT
> > +#define PCI_EXT_CAP_ID_DOE     0x2E    /* Data Object Exchange */
> > +#define PCI_EXT_CAP_ID_MAX     PCI_EXT_CAP_ID_DOE
> >
> >  #define PCI_EXT_CAP_DSN_SIZEOF 12
> >  #define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40
> > @@ -1098,4 +1099,30 @@
> >  #define  PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_MASK                0x000000F0
> >  #define  PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_SHIFT       4
> >
> > +/* Data Object Exchange */
> > +#define PCI_DOE_CAP            0x04    /* DOE Capabilities Register */
> > +#define  PCI_DOE_CAP_INT                       0x00000001  /* Interrupt Support */
> > +#define  PCI_DOE_CAP_IRQ                       0x00000ffe  /* Interrupt Message Number */
> > +#define PCI_DOE_CTRL           0x08    /* DOE Control Register */
> > +#define  PCI_DOE_CTRL_ABORT                    0x00000001  /* DOE Abort */
> > +#define  PCI_DOE_CTRL_INT_EN                   0x00000002  /* DOE Interrupt Enable */
> > +#define  PCI_DOE_CTRL_GO                       0x80000000  /* DOE Go */
> > +#define PCI_DOE_STATUS         0x0c    /* DOE Status Register */
> > +#define  PCI_DOE_STATUS_BUSY                   0x00000001  /* DOE Busy */
> > +#define  PCI_DOE_STATUS_INT_STATUS             0x00000002  /* DOE Interrupt Status */
> > +#define  PCI_DOE_STATUS_ERROR                  0x00000004  /* DOE Error */
> > +#define  PCI_DOE_STATUS_DATA_OBJECT_READY      0x80000000  /* Data Object Ready */
> > +#define PCI_DOE_WRITE          0x10    /* DOE Write Data Mailbox Register */
> > +#define PCI_DOE_READ           0x14    /* DOE Read Data Mailbox Register */
> > +
> > +/* DOE Data Object - note not actually registers */
> > +#define PCI_DOE_DATA_OBJECT_HEADER_1_VID               0x0000ffff
> > +#define PCI_DOE_DATA_OBJECT_HEADER_1_TYPE              0x00ff0000
> > +#define PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH            0x0003ffff
> > +
> > +#define PCI_DOE_DATA_OBJECT_DISC_REQ_3_INDEX           0x000000ff
> > +#define PCI_DOE_DATA_OBJECT_DISC_RSP_3_VID             0x0000ffff
> > +#define PCI_DOE_DATA_OBJECT_DISC_RSP_3_PROTOCOL                0x00ff0000
> > +#define PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX      0xff000000
> > +
> >  #endif /* LINUX_PCI_REGS_H */
> > --
> > 2.31.1
> >  




[Index of Archives]     [DMA Engine]     [Linux Coverity]     [Linux USB]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Greybus]

  Powered by Linux