189308c11SAndra Paraschiv // SPDX-License-Identifier: GPL-2.0
289308c11SAndra Paraschiv /*
3*e3cba4d2SAndra Paraschiv * Copyright 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
489308c11SAndra Paraschiv */
589308c11SAndra Paraschiv
689308c11SAndra Paraschiv /**
789308c11SAndra Paraschiv * DOC: Nitro Enclaves (NE) PCI device driver.
889308c11SAndra Paraschiv */
989308c11SAndra Paraschiv
1089308c11SAndra Paraschiv #include <linux/delay.h>
1189308c11SAndra Paraschiv #include <linux/device.h>
1289308c11SAndra Paraschiv #include <linux/list.h>
1389308c11SAndra Paraschiv #include <linux/module.h>
1489308c11SAndra Paraschiv #include <linux/mutex.h>
1589308c11SAndra Paraschiv #include <linux/nitro_enclaves.h>
1689308c11SAndra Paraschiv #include <linux/pci.h>
1789308c11SAndra Paraschiv #include <linux/types.h>
1889308c11SAndra Paraschiv #include <linux/wait.h>
1989308c11SAndra Paraschiv
2089308c11SAndra Paraschiv #include "ne_misc_dev.h"
2189308c11SAndra Paraschiv #include "ne_pci_dev.h"
2289308c11SAndra Paraschiv
2389308c11SAndra Paraschiv /**
2489308c11SAndra Paraschiv * NE_DEFAULT_TIMEOUT_MSECS - Default timeout to wait for a reply from
2589308c11SAndra Paraschiv * the NE PCI device.
2689308c11SAndra Paraschiv */
2789308c11SAndra Paraschiv #define NE_DEFAULT_TIMEOUT_MSECS (120000) /* 120 sec */
2889308c11SAndra Paraschiv
2989308c11SAndra Paraschiv static const struct pci_device_id ne_pci_ids[] = {
3089308c11SAndra Paraschiv { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_NE) },
3189308c11SAndra Paraschiv { 0, }
3289308c11SAndra Paraschiv };
3389308c11SAndra Paraschiv
3489308c11SAndra Paraschiv MODULE_DEVICE_TABLE(pci, ne_pci_ids);
3589308c11SAndra Paraschiv
3689308c11SAndra Paraschiv /**
37ad2b6980SAndra Paraschiv * ne_submit_request() - Submit command request to the PCI device based on the
38ad2b6980SAndra Paraschiv * command type.
39ad2b6980SAndra Paraschiv * @pdev: PCI device to send the command to.
40ad2b6980SAndra Paraschiv * @cmd_type: Command type of the request sent to the PCI device.
41ad2b6980SAndra Paraschiv * @cmd_request: Command request payload.
42ad2b6980SAndra Paraschiv * @cmd_request_size: Size of the command request payload.
43ad2b6980SAndra Paraschiv *
44ad2b6980SAndra Paraschiv * Context: Process context. This function is called with the ne_pci_dev mutex held.
45ad2b6980SAndra Paraschiv */
ne_submit_request(struct pci_dev * pdev,enum ne_pci_dev_cmd_type cmd_type,void * cmd_request,size_t cmd_request_size)46ad2b6980SAndra Paraschiv static void ne_submit_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type,
47ad2b6980SAndra Paraschiv void *cmd_request, size_t cmd_request_size)
48ad2b6980SAndra Paraschiv {
49ad2b6980SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
50ad2b6980SAndra Paraschiv
51ad2b6980SAndra Paraschiv memcpy_toio(ne_pci_dev->iomem_base + NE_SEND_DATA, cmd_request, cmd_request_size);
52ad2b6980SAndra Paraschiv
53ad2b6980SAndra Paraschiv iowrite32(cmd_type, ne_pci_dev->iomem_base + NE_COMMAND);
54ad2b6980SAndra Paraschiv }
55ad2b6980SAndra Paraschiv
56ad2b6980SAndra Paraschiv /**
57ad2b6980SAndra Paraschiv * ne_retrieve_reply() - Retrieve reply from the PCI device.
58ad2b6980SAndra Paraschiv * @pdev: PCI device to receive the reply from.
59ad2b6980SAndra Paraschiv * @cmd_reply: Command reply payload.
60ad2b6980SAndra Paraschiv * @cmd_reply_size: Size of the command reply payload.
61ad2b6980SAndra Paraschiv *
62ad2b6980SAndra Paraschiv * Context: Process context. This function is called with the ne_pci_dev mutex held.
63ad2b6980SAndra Paraschiv */
ne_retrieve_reply(struct pci_dev * pdev,struct ne_pci_dev_cmd_reply * cmd_reply,size_t cmd_reply_size)64ad2b6980SAndra Paraschiv static void ne_retrieve_reply(struct pci_dev *pdev, struct ne_pci_dev_cmd_reply *cmd_reply,
65ad2b6980SAndra Paraschiv size_t cmd_reply_size)
66ad2b6980SAndra Paraschiv {
67ad2b6980SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
68ad2b6980SAndra Paraschiv
69ad2b6980SAndra Paraschiv memcpy_fromio(cmd_reply, ne_pci_dev->iomem_base + NE_RECV_DATA, cmd_reply_size);
70ad2b6980SAndra Paraschiv }
71ad2b6980SAndra Paraschiv
72ad2b6980SAndra Paraschiv /**
73ad2b6980SAndra Paraschiv * ne_wait_for_reply() - Wait for a reply of a PCI device command.
74ad2b6980SAndra Paraschiv * @pdev: PCI device for which a reply is waited.
75ad2b6980SAndra Paraschiv *
76ad2b6980SAndra Paraschiv * Context: Process context. This function is called with the ne_pci_dev mutex held.
77ad2b6980SAndra Paraschiv * Return:
78ad2b6980SAndra Paraschiv * * 0 on success.
79ad2b6980SAndra Paraschiv * * Negative return value on failure.
80ad2b6980SAndra Paraschiv */
ne_wait_for_reply(struct pci_dev * pdev)81ad2b6980SAndra Paraschiv static int ne_wait_for_reply(struct pci_dev *pdev)
82ad2b6980SAndra Paraschiv {
83ad2b6980SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
84ad2b6980SAndra Paraschiv int rc = -EINVAL;
85ad2b6980SAndra Paraschiv
86ad2b6980SAndra Paraschiv /*
87ad2b6980SAndra Paraschiv * TODO: Update to _interruptible and handle interrupted wait event
88ad2b6980SAndra Paraschiv * e.g. -ERESTARTSYS, incoming signals + update timeout, if needed.
89ad2b6980SAndra Paraschiv */
90ad2b6980SAndra Paraschiv rc = wait_event_timeout(ne_pci_dev->cmd_reply_wait_q,
91ad2b6980SAndra Paraschiv atomic_read(&ne_pci_dev->cmd_reply_avail) != 0,
92ad2b6980SAndra Paraschiv msecs_to_jiffies(NE_DEFAULT_TIMEOUT_MSECS));
93ad2b6980SAndra Paraschiv if (!rc)
94ad2b6980SAndra Paraschiv return -ETIMEDOUT;
95ad2b6980SAndra Paraschiv
96ad2b6980SAndra Paraschiv return 0;
97ad2b6980SAndra Paraschiv }
98ad2b6980SAndra Paraschiv
ne_do_request(struct pci_dev * pdev,enum ne_pci_dev_cmd_type cmd_type,void * cmd_request,size_t cmd_request_size,struct ne_pci_dev_cmd_reply * cmd_reply,size_t cmd_reply_size)99ad2b6980SAndra Paraschiv int ne_do_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type,
100ad2b6980SAndra Paraschiv void *cmd_request, size_t cmd_request_size,
101ad2b6980SAndra Paraschiv struct ne_pci_dev_cmd_reply *cmd_reply, size_t cmd_reply_size)
102ad2b6980SAndra Paraschiv {
103ad2b6980SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
104ad2b6980SAndra Paraschiv int rc = -EINVAL;
105ad2b6980SAndra Paraschiv
106ad2b6980SAndra Paraschiv if (cmd_type <= INVALID_CMD || cmd_type >= MAX_CMD) {
107ad2b6980SAndra Paraschiv dev_err_ratelimited(&pdev->dev, "Invalid cmd type=%u\n", cmd_type);
108ad2b6980SAndra Paraschiv
109ad2b6980SAndra Paraschiv return -EINVAL;
110ad2b6980SAndra Paraschiv }
111ad2b6980SAndra Paraschiv
112ad2b6980SAndra Paraschiv if (!cmd_request) {
113ad2b6980SAndra Paraschiv dev_err_ratelimited(&pdev->dev, "Null cmd request for cmd type=%u\n",
114ad2b6980SAndra Paraschiv cmd_type);
115ad2b6980SAndra Paraschiv
116ad2b6980SAndra Paraschiv return -EINVAL;
117ad2b6980SAndra Paraschiv }
118ad2b6980SAndra Paraschiv
119ad2b6980SAndra Paraschiv if (cmd_request_size > NE_SEND_DATA_SIZE) {
120ad2b6980SAndra Paraschiv dev_err_ratelimited(&pdev->dev, "Invalid req size=%zu for cmd type=%u\n",
121ad2b6980SAndra Paraschiv cmd_request_size, cmd_type);
122ad2b6980SAndra Paraschiv
123ad2b6980SAndra Paraschiv return -EINVAL;
124ad2b6980SAndra Paraschiv }
125ad2b6980SAndra Paraschiv
126ad2b6980SAndra Paraschiv if (!cmd_reply) {
127ad2b6980SAndra Paraschiv dev_err_ratelimited(&pdev->dev, "Null cmd reply for cmd type=%u\n",
128ad2b6980SAndra Paraschiv cmd_type);
129ad2b6980SAndra Paraschiv
130ad2b6980SAndra Paraschiv return -EINVAL;
131ad2b6980SAndra Paraschiv }
132ad2b6980SAndra Paraschiv
133ad2b6980SAndra Paraschiv if (cmd_reply_size > NE_RECV_DATA_SIZE) {
134ad2b6980SAndra Paraschiv dev_err_ratelimited(&pdev->dev, "Invalid reply size=%zu for cmd type=%u\n",
135ad2b6980SAndra Paraschiv cmd_reply_size, cmd_type);
136ad2b6980SAndra Paraschiv
137ad2b6980SAndra Paraschiv return -EINVAL;
138ad2b6980SAndra Paraschiv }
139ad2b6980SAndra Paraschiv
140ad2b6980SAndra Paraschiv /*
141ad2b6980SAndra Paraschiv * Use this mutex so that the PCI device handles one command request at
142ad2b6980SAndra Paraschiv * a time.
143ad2b6980SAndra Paraschiv */
144ad2b6980SAndra Paraschiv mutex_lock(&ne_pci_dev->pci_dev_mutex);
145ad2b6980SAndra Paraschiv
146ad2b6980SAndra Paraschiv atomic_set(&ne_pci_dev->cmd_reply_avail, 0);
147ad2b6980SAndra Paraschiv
148ad2b6980SAndra Paraschiv ne_submit_request(pdev, cmd_type, cmd_request, cmd_request_size);
149ad2b6980SAndra Paraschiv
150ad2b6980SAndra Paraschiv rc = ne_wait_for_reply(pdev);
151ad2b6980SAndra Paraschiv if (rc < 0) {
152ad2b6980SAndra Paraschiv dev_err_ratelimited(&pdev->dev, "Error in wait for reply for cmd type=%u [rc=%d]\n",
153ad2b6980SAndra Paraschiv cmd_type, rc);
154ad2b6980SAndra Paraschiv
155ad2b6980SAndra Paraschiv goto unlock_mutex;
156ad2b6980SAndra Paraschiv }
157ad2b6980SAndra Paraschiv
158ad2b6980SAndra Paraschiv ne_retrieve_reply(pdev, cmd_reply, cmd_reply_size);
159ad2b6980SAndra Paraschiv
160ad2b6980SAndra Paraschiv atomic_set(&ne_pci_dev->cmd_reply_avail, 0);
161ad2b6980SAndra Paraschiv
162ad2b6980SAndra Paraschiv if (cmd_reply->rc < 0) {
163ad2b6980SAndra Paraschiv rc = cmd_reply->rc;
164ad2b6980SAndra Paraschiv
165ad2b6980SAndra Paraschiv dev_err_ratelimited(&pdev->dev, "Error in cmd process logic, cmd type=%u [rc=%d]\n",
166ad2b6980SAndra Paraschiv cmd_type, rc);
167ad2b6980SAndra Paraschiv
168ad2b6980SAndra Paraschiv goto unlock_mutex;
169ad2b6980SAndra Paraschiv }
170ad2b6980SAndra Paraschiv
171ad2b6980SAndra Paraschiv rc = 0;
172ad2b6980SAndra Paraschiv
173ad2b6980SAndra Paraschiv unlock_mutex:
174ad2b6980SAndra Paraschiv mutex_unlock(&ne_pci_dev->pci_dev_mutex);
175ad2b6980SAndra Paraschiv
176ad2b6980SAndra Paraschiv return rc;
177ad2b6980SAndra Paraschiv }
178ad2b6980SAndra Paraschiv
179ad2b6980SAndra Paraschiv /**
180ad2b6980SAndra Paraschiv * ne_reply_handler() - Interrupt handler for retrieving a reply matching a
181ad2b6980SAndra Paraschiv * request sent to the PCI device for enclave lifetime
182ad2b6980SAndra Paraschiv * management.
183ad2b6980SAndra Paraschiv * @irq: Received interrupt for a reply sent by the PCI device.
184ad2b6980SAndra Paraschiv * @args: PCI device private data structure.
185ad2b6980SAndra Paraschiv *
186ad2b6980SAndra Paraschiv * Context: Interrupt context.
187ad2b6980SAndra Paraschiv * Return:
188ad2b6980SAndra Paraschiv * * IRQ_HANDLED on handled interrupt.
189ad2b6980SAndra Paraschiv */
ne_reply_handler(int irq,void * args)190ad2b6980SAndra Paraschiv static irqreturn_t ne_reply_handler(int irq, void *args)
191ad2b6980SAndra Paraschiv {
192ad2b6980SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = (struct ne_pci_dev *)args;
193ad2b6980SAndra Paraschiv
194ad2b6980SAndra Paraschiv atomic_set(&ne_pci_dev->cmd_reply_avail, 1);
195ad2b6980SAndra Paraschiv
196ad2b6980SAndra Paraschiv /* TODO: Update to _interruptible. */
197ad2b6980SAndra Paraschiv wake_up(&ne_pci_dev->cmd_reply_wait_q);
198ad2b6980SAndra Paraschiv
199ad2b6980SAndra Paraschiv return IRQ_HANDLED;
200ad2b6980SAndra Paraschiv }
201ad2b6980SAndra Paraschiv
202ad2b6980SAndra Paraschiv /**
203e5d616d8SAndra Paraschiv * ne_event_work_handler() - Work queue handler for notifying enclaves on a
204e5d616d8SAndra Paraschiv * state change received by the event interrupt
205e5d616d8SAndra Paraschiv * handler.
206e5d616d8SAndra Paraschiv * @work: Item containing the NE PCI device for which an out-of-band event
207e5d616d8SAndra Paraschiv * was issued.
208e5d616d8SAndra Paraschiv *
209e5d616d8SAndra Paraschiv * An out-of-band event is being issued by the Nitro Hypervisor when at least
210e5d616d8SAndra Paraschiv * one enclave is changing state without client interaction.
211e5d616d8SAndra Paraschiv *
212e5d616d8SAndra Paraschiv * Context: Work queue context.
213e5d616d8SAndra Paraschiv */
ne_event_work_handler(struct work_struct * work)214e5d616d8SAndra Paraschiv static void ne_event_work_handler(struct work_struct *work)
215e5d616d8SAndra Paraschiv {
216e5d616d8SAndra Paraschiv struct ne_pci_dev_cmd_reply cmd_reply = {};
217e5d616d8SAndra Paraschiv struct ne_enclave *ne_enclave = NULL;
218e5d616d8SAndra Paraschiv struct ne_pci_dev *ne_pci_dev =
219e5d616d8SAndra Paraschiv container_of(work, struct ne_pci_dev, notify_work);
220e5d616d8SAndra Paraschiv struct pci_dev *pdev = ne_pci_dev->pdev;
221e5d616d8SAndra Paraschiv int rc = -EINVAL;
222e5d616d8SAndra Paraschiv struct slot_info_req slot_info_req = {};
223e5d616d8SAndra Paraschiv
224e5d616d8SAndra Paraschiv mutex_lock(&ne_pci_dev->enclaves_list_mutex);
225e5d616d8SAndra Paraschiv
226e5d616d8SAndra Paraschiv /*
227e5d616d8SAndra Paraschiv * Iterate over all enclaves registered for the Nitro Enclaves
228e5d616d8SAndra Paraschiv * PCI device and determine for which enclave(s) the out-of-band event
229e5d616d8SAndra Paraschiv * is corresponding to.
230e5d616d8SAndra Paraschiv */
231e5d616d8SAndra Paraschiv list_for_each_entry(ne_enclave, &ne_pci_dev->enclaves_list, enclave_list_entry) {
232e5d616d8SAndra Paraschiv mutex_lock(&ne_enclave->enclave_info_mutex);
233e5d616d8SAndra Paraschiv
234e5d616d8SAndra Paraschiv /*
235e5d616d8SAndra Paraschiv * Enclaves that were never started cannot receive out-of-band
236e5d616d8SAndra Paraschiv * events.
237e5d616d8SAndra Paraschiv */
238e5d616d8SAndra Paraschiv if (ne_enclave->state != NE_STATE_RUNNING)
239e5d616d8SAndra Paraschiv goto unlock;
240e5d616d8SAndra Paraschiv
241e5d616d8SAndra Paraschiv slot_info_req.slot_uid = ne_enclave->slot_uid;
242e5d616d8SAndra Paraschiv
243e5d616d8SAndra Paraschiv rc = ne_do_request(pdev, SLOT_INFO,
244e5d616d8SAndra Paraschiv &slot_info_req, sizeof(slot_info_req),
245e5d616d8SAndra Paraschiv &cmd_reply, sizeof(cmd_reply));
246e5d616d8SAndra Paraschiv if (rc < 0)
247e5d616d8SAndra Paraschiv dev_err(&pdev->dev, "Error in slot info [rc=%d]\n", rc);
248e5d616d8SAndra Paraschiv
249e5d616d8SAndra Paraschiv /* Notify enclave process that the enclave state changed. */
250e5d616d8SAndra Paraschiv if (ne_enclave->state != cmd_reply.state) {
251e5d616d8SAndra Paraschiv ne_enclave->state = cmd_reply.state;
252e5d616d8SAndra Paraschiv
253e5d616d8SAndra Paraschiv ne_enclave->has_event = true;
254e5d616d8SAndra Paraschiv
255e5d616d8SAndra Paraschiv wake_up_interruptible(&ne_enclave->eventq);
256e5d616d8SAndra Paraschiv }
257e5d616d8SAndra Paraschiv
258e5d616d8SAndra Paraschiv unlock:
259e5d616d8SAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
260e5d616d8SAndra Paraschiv }
261e5d616d8SAndra Paraschiv
262e5d616d8SAndra Paraschiv mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
263e5d616d8SAndra Paraschiv }
264e5d616d8SAndra Paraschiv
265e5d616d8SAndra Paraschiv /**
266e5d616d8SAndra Paraschiv * ne_event_handler() - Interrupt handler for PCI device out-of-band events.
267e5d616d8SAndra Paraschiv * This interrupt does not supply any data in the MMIO
268e5d616d8SAndra Paraschiv * region. It notifies a change in the state of any of
269e5d616d8SAndra Paraschiv * the launched enclaves.
270e5d616d8SAndra Paraschiv * @irq: Received interrupt for an out-of-band event.
271e5d616d8SAndra Paraschiv * @args: PCI device private data structure.
272e5d616d8SAndra Paraschiv *
273e5d616d8SAndra Paraschiv * Context: Interrupt context.
274e5d616d8SAndra Paraschiv * Return:
275e5d616d8SAndra Paraschiv * * IRQ_HANDLED on handled interrupt.
276e5d616d8SAndra Paraschiv */
ne_event_handler(int irq,void * args)277e5d616d8SAndra Paraschiv static irqreturn_t ne_event_handler(int irq, void *args)
278e5d616d8SAndra Paraschiv {
279e5d616d8SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = (struct ne_pci_dev *)args;
280e5d616d8SAndra Paraschiv
281e5d616d8SAndra Paraschiv queue_work(ne_pci_dev->event_wq, &ne_pci_dev->notify_work);
282e5d616d8SAndra Paraschiv
283e5d616d8SAndra Paraschiv return IRQ_HANDLED;
284e5d616d8SAndra Paraschiv }
285e5d616d8SAndra Paraschiv
286e5d616d8SAndra Paraschiv /**
28789308c11SAndra Paraschiv * ne_setup_msix() - Setup MSI-X vectors for the PCI device.
28889308c11SAndra Paraschiv * @pdev: PCI device to setup the MSI-X for.
28989308c11SAndra Paraschiv *
29089308c11SAndra Paraschiv * Context: Process context.
29189308c11SAndra Paraschiv * Return:
29289308c11SAndra Paraschiv * * 0 on success.
29389308c11SAndra Paraschiv * * Negative return value on failure.
29489308c11SAndra Paraschiv */
ne_setup_msix(struct pci_dev * pdev)29589308c11SAndra Paraschiv static int ne_setup_msix(struct pci_dev *pdev)
29689308c11SAndra Paraschiv {
297ad2b6980SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
29889308c11SAndra Paraschiv int nr_vecs = 0;
29989308c11SAndra Paraschiv int rc = -EINVAL;
30089308c11SAndra Paraschiv
30189308c11SAndra Paraschiv nr_vecs = pci_msix_vec_count(pdev);
30289308c11SAndra Paraschiv if (nr_vecs < 0) {
30389308c11SAndra Paraschiv rc = nr_vecs;
30489308c11SAndra Paraschiv
30589308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in getting vec count [rc=%d]\n", rc);
30689308c11SAndra Paraschiv
30789308c11SAndra Paraschiv return rc;
30889308c11SAndra Paraschiv }
30989308c11SAndra Paraschiv
31089308c11SAndra Paraschiv rc = pci_alloc_irq_vectors(pdev, nr_vecs, nr_vecs, PCI_IRQ_MSIX);
31189308c11SAndra Paraschiv if (rc < 0) {
31289308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in alloc MSI-X vecs [rc=%d]\n", rc);
31389308c11SAndra Paraschiv
31489308c11SAndra Paraschiv return rc;
31589308c11SAndra Paraschiv }
31689308c11SAndra Paraschiv
317ad2b6980SAndra Paraschiv /*
318ad2b6980SAndra Paraschiv * This IRQ gets triggered every time the PCI device responds to a
319ad2b6980SAndra Paraschiv * command request. The reply is then retrieved, reading from the MMIO
320ad2b6980SAndra Paraschiv * space of the PCI device.
321ad2b6980SAndra Paraschiv */
322ad2b6980SAndra Paraschiv rc = request_irq(pci_irq_vector(pdev, NE_VEC_REPLY), ne_reply_handler,
323ad2b6980SAndra Paraschiv 0, "enclave_cmd", ne_pci_dev);
324ad2b6980SAndra Paraschiv if (rc < 0) {
325ad2b6980SAndra Paraschiv dev_err(&pdev->dev, "Error in request irq reply [rc=%d]\n", rc);
326ad2b6980SAndra Paraschiv
327ad2b6980SAndra Paraschiv goto free_irq_vectors;
328ad2b6980SAndra Paraschiv }
329ad2b6980SAndra Paraschiv
330e5d616d8SAndra Paraschiv ne_pci_dev->event_wq = create_singlethread_workqueue("ne_pci_dev_wq");
331e5d616d8SAndra Paraschiv if (!ne_pci_dev->event_wq) {
332e5d616d8SAndra Paraschiv rc = -ENOMEM;
333e5d616d8SAndra Paraschiv
334e5d616d8SAndra Paraschiv dev_err(&pdev->dev, "Cannot get wq for dev events [rc=%d]\n", rc);
335e5d616d8SAndra Paraschiv
336e5d616d8SAndra Paraschiv goto free_reply_irq_vec;
337e5d616d8SAndra Paraschiv }
338e5d616d8SAndra Paraschiv
339e5d616d8SAndra Paraschiv INIT_WORK(&ne_pci_dev->notify_work, ne_event_work_handler);
340e5d616d8SAndra Paraschiv
341e5d616d8SAndra Paraschiv /*
342e5d616d8SAndra Paraschiv * This IRQ gets triggered every time any enclave's state changes. Its
343e5d616d8SAndra Paraschiv * handler then scans for the changes and propagates them to the user
344e5d616d8SAndra Paraschiv * space.
345e5d616d8SAndra Paraschiv */
346e5d616d8SAndra Paraschiv rc = request_irq(pci_irq_vector(pdev, NE_VEC_EVENT), ne_event_handler,
347e5d616d8SAndra Paraschiv 0, "enclave_evt", ne_pci_dev);
348e5d616d8SAndra Paraschiv if (rc < 0) {
349e5d616d8SAndra Paraschiv dev_err(&pdev->dev, "Error in request irq event [rc=%d]\n", rc);
350e5d616d8SAndra Paraschiv
351e5d616d8SAndra Paraschiv goto destroy_wq;
352e5d616d8SAndra Paraschiv }
353e5d616d8SAndra Paraschiv
35489308c11SAndra Paraschiv return 0;
355ad2b6980SAndra Paraschiv
356e5d616d8SAndra Paraschiv destroy_wq:
357e5d616d8SAndra Paraschiv destroy_workqueue(ne_pci_dev->event_wq);
358e5d616d8SAndra Paraschiv free_reply_irq_vec:
359e5d616d8SAndra Paraschiv free_irq(pci_irq_vector(pdev, NE_VEC_REPLY), ne_pci_dev);
360ad2b6980SAndra Paraschiv free_irq_vectors:
361ad2b6980SAndra Paraschiv pci_free_irq_vectors(pdev);
362ad2b6980SAndra Paraschiv
363ad2b6980SAndra Paraschiv return rc;
36489308c11SAndra Paraschiv }
36589308c11SAndra Paraschiv
36689308c11SAndra Paraschiv /**
36789308c11SAndra Paraschiv * ne_teardown_msix() - Teardown MSI-X vectors for the PCI device.
36889308c11SAndra Paraschiv * @pdev: PCI device to teardown the MSI-X for.
36989308c11SAndra Paraschiv *
37089308c11SAndra Paraschiv * Context: Process context.
37189308c11SAndra Paraschiv */
ne_teardown_msix(struct pci_dev * pdev)37289308c11SAndra Paraschiv static void ne_teardown_msix(struct pci_dev *pdev)
37389308c11SAndra Paraschiv {
374ad2b6980SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
375ad2b6980SAndra Paraschiv
376e5d616d8SAndra Paraschiv free_irq(pci_irq_vector(pdev, NE_VEC_EVENT), ne_pci_dev);
377e5d616d8SAndra Paraschiv
378e5d616d8SAndra Paraschiv flush_work(&ne_pci_dev->notify_work);
379e5d616d8SAndra Paraschiv destroy_workqueue(ne_pci_dev->event_wq);
380e5d616d8SAndra Paraschiv
381ad2b6980SAndra Paraschiv free_irq(pci_irq_vector(pdev, NE_VEC_REPLY), ne_pci_dev);
382ad2b6980SAndra Paraschiv
38389308c11SAndra Paraschiv pci_free_irq_vectors(pdev);
38489308c11SAndra Paraschiv }
38589308c11SAndra Paraschiv
38689308c11SAndra Paraschiv /**
38789308c11SAndra Paraschiv * ne_pci_dev_enable() - Select the PCI device version and enable it.
38889308c11SAndra Paraschiv * @pdev: PCI device to select version for and then enable.
38989308c11SAndra Paraschiv *
39089308c11SAndra Paraschiv * Context: Process context.
39189308c11SAndra Paraschiv * Return:
39289308c11SAndra Paraschiv * * 0 on success.
39389308c11SAndra Paraschiv * * Negative return value on failure.
39489308c11SAndra Paraschiv */
ne_pci_dev_enable(struct pci_dev * pdev)39589308c11SAndra Paraschiv static int ne_pci_dev_enable(struct pci_dev *pdev)
39689308c11SAndra Paraschiv {
39789308c11SAndra Paraschiv u8 dev_enable_reply = 0;
39889308c11SAndra Paraschiv u16 dev_version_reply = 0;
39989308c11SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
40089308c11SAndra Paraschiv
40189308c11SAndra Paraschiv iowrite16(NE_VERSION_MAX, ne_pci_dev->iomem_base + NE_VERSION);
40289308c11SAndra Paraschiv
40389308c11SAndra Paraschiv dev_version_reply = ioread16(ne_pci_dev->iomem_base + NE_VERSION);
40489308c11SAndra Paraschiv if (dev_version_reply != NE_VERSION_MAX) {
40589308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in pci dev version cmd\n");
40689308c11SAndra Paraschiv
40789308c11SAndra Paraschiv return -EIO;
40889308c11SAndra Paraschiv }
40989308c11SAndra Paraschiv
41089308c11SAndra Paraschiv iowrite8(NE_ENABLE_ON, ne_pci_dev->iomem_base + NE_ENABLE);
41189308c11SAndra Paraschiv
41289308c11SAndra Paraschiv dev_enable_reply = ioread8(ne_pci_dev->iomem_base + NE_ENABLE);
41389308c11SAndra Paraschiv if (dev_enable_reply != NE_ENABLE_ON) {
41489308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in pci dev enable cmd\n");
41589308c11SAndra Paraschiv
41689308c11SAndra Paraschiv return -EIO;
41789308c11SAndra Paraschiv }
41889308c11SAndra Paraschiv
41989308c11SAndra Paraschiv return 0;
42089308c11SAndra Paraschiv }
42189308c11SAndra Paraschiv
42289308c11SAndra Paraschiv /**
42389308c11SAndra Paraschiv * ne_pci_dev_disable() - Disable the PCI device.
42489308c11SAndra Paraschiv * @pdev: PCI device to disable.
42589308c11SAndra Paraschiv *
42689308c11SAndra Paraschiv * Context: Process context.
42789308c11SAndra Paraschiv */
ne_pci_dev_disable(struct pci_dev * pdev)42889308c11SAndra Paraschiv static void ne_pci_dev_disable(struct pci_dev *pdev)
42989308c11SAndra Paraschiv {
43089308c11SAndra Paraschiv u8 dev_disable_reply = 0;
43189308c11SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
43289308c11SAndra Paraschiv const unsigned int sleep_time = 10; /* 10 ms */
43389308c11SAndra Paraschiv unsigned int sleep_time_count = 0;
43489308c11SAndra Paraschiv
43589308c11SAndra Paraschiv iowrite8(NE_ENABLE_OFF, ne_pci_dev->iomem_base + NE_ENABLE);
43689308c11SAndra Paraschiv
43789308c11SAndra Paraschiv /*
43889308c11SAndra Paraschiv * Check for NE_ENABLE_OFF in a loop, to handle cases when the device
43989308c11SAndra Paraschiv * state is not immediately set to disabled and going through a
44089308c11SAndra Paraschiv * transitory state of disabling.
44189308c11SAndra Paraschiv */
44289308c11SAndra Paraschiv while (sleep_time_count < NE_DEFAULT_TIMEOUT_MSECS) {
44389308c11SAndra Paraschiv dev_disable_reply = ioread8(ne_pci_dev->iomem_base + NE_ENABLE);
44489308c11SAndra Paraschiv if (dev_disable_reply == NE_ENABLE_OFF)
44589308c11SAndra Paraschiv return;
44689308c11SAndra Paraschiv
44789308c11SAndra Paraschiv msleep_interruptible(sleep_time);
44889308c11SAndra Paraschiv sleep_time_count += sleep_time;
44989308c11SAndra Paraschiv }
45089308c11SAndra Paraschiv
45189308c11SAndra Paraschiv dev_disable_reply = ioread8(ne_pci_dev->iomem_base + NE_ENABLE);
45289308c11SAndra Paraschiv if (dev_disable_reply != NE_ENABLE_OFF)
45389308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in pci dev disable cmd\n");
45489308c11SAndra Paraschiv }
45589308c11SAndra Paraschiv
45689308c11SAndra Paraschiv /**
45789308c11SAndra Paraschiv * ne_pci_probe() - Probe function for the NE PCI device.
45889308c11SAndra Paraschiv * @pdev: PCI device to match with the NE PCI driver.
45989308c11SAndra Paraschiv * @id : PCI device id table associated with the NE PCI driver.
46089308c11SAndra Paraschiv *
46189308c11SAndra Paraschiv * Context: Process context.
46289308c11SAndra Paraschiv * Return:
46389308c11SAndra Paraschiv * * 0 on success.
46489308c11SAndra Paraschiv * * Negative return value on failure.
46589308c11SAndra Paraschiv */
ne_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)46689308c11SAndra Paraschiv static int ne_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
46789308c11SAndra Paraschiv {
46889308c11SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = NULL;
46989308c11SAndra Paraschiv int rc = -EINVAL;
47089308c11SAndra Paraschiv
47189308c11SAndra Paraschiv ne_pci_dev = kzalloc(sizeof(*ne_pci_dev), GFP_KERNEL);
47289308c11SAndra Paraschiv if (!ne_pci_dev)
47389308c11SAndra Paraschiv return -ENOMEM;
47489308c11SAndra Paraschiv
47589308c11SAndra Paraschiv rc = pci_enable_device(pdev);
47689308c11SAndra Paraschiv if (rc < 0) {
47789308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in pci dev enable [rc=%d]\n", rc);
47889308c11SAndra Paraschiv
47989308c11SAndra Paraschiv goto free_ne_pci_dev;
48089308c11SAndra Paraschiv }
48189308c11SAndra Paraschiv
482d874742fSLongpeng(Mike) pci_set_master(pdev);
483d874742fSLongpeng(Mike)
48489308c11SAndra Paraschiv rc = pci_request_regions_exclusive(pdev, "nitro_enclaves");
48589308c11SAndra Paraschiv if (rc < 0) {
48689308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in pci request regions [rc=%d]\n", rc);
48789308c11SAndra Paraschiv
48889308c11SAndra Paraschiv goto disable_pci_dev;
48989308c11SAndra Paraschiv }
49089308c11SAndra Paraschiv
49189308c11SAndra Paraschiv ne_pci_dev->iomem_base = pci_iomap(pdev, PCI_BAR_NE, 0);
49289308c11SAndra Paraschiv if (!ne_pci_dev->iomem_base) {
49389308c11SAndra Paraschiv rc = -ENOMEM;
49489308c11SAndra Paraschiv
49589308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in pci iomap [rc=%d]\n", rc);
49689308c11SAndra Paraschiv
49789308c11SAndra Paraschiv goto release_pci_regions;
49889308c11SAndra Paraschiv }
49989308c11SAndra Paraschiv
50089308c11SAndra Paraschiv pci_set_drvdata(pdev, ne_pci_dev);
50189308c11SAndra Paraschiv
50289308c11SAndra Paraschiv rc = ne_setup_msix(pdev);
50389308c11SAndra Paraschiv if (rc < 0) {
50489308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in pci dev msix setup [rc=%d]\n", rc);
50589308c11SAndra Paraschiv
50689308c11SAndra Paraschiv goto iounmap_pci_bar;
50789308c11SAndra Paraschiv }
50889308c11SAndra Paraschiv
50989308c11SAndra Paraschiv ne_pci_dev_disable(pdev);
51089308c11SAndra Paraschiv
51189308c11SAndra Paraschiv rc = ne_pci_dev_enable(pdev);
51289308c11SAndra Paraschiv if (rc < 0) {
51389308c11SAndra Paraschiv dev_err(&pdev->dev, "Error in ne_pci_dev enable [rc=%d]\n", rc);
51489308c11SAndra Paraschiv
51589308c11SAndra Paraschiv goto teardown_msix;
51689308c11SAndra Paraschiv }
51789308c11SAndra Paraschiv
51889308c11SAndra Paraschiv atomic_set(&ne_pci_dev->cmd_reply_avail, 0);
51989308c11SAndra Paraschiv init_waitqueue_head(&ne_pci_dev->cmd_reply_wait_q);
52089308c11SAndra Paraschiv INIT_LIST_HEAD(&ne_pci_dev->enclaves_list);
52189308c11SAndra Paraschiv mutex_init(&ne_pci_dev->enclaves_list_mutex);
52289308c11SAndra Paraschiv mutex_init(&ne_pci_dev->pci_dev_mutex);
52389308c11SAndra Paraschiv ne_pci_dev->pdev = pdev;
52489308c11SAndra Paraschiv
52589308c11SAndra Paraschiv ne_devs.ne_pci_dev = ne_pci_dev;
52689308c11SAndra Paraschiv
527bd47c995SAndra Paraschiv rc = misc_register(ne_devs.ne_misc_dev);
528bd47c995SAndra Paraschiv if (rc < 0) {
529bd47c995SAndra Paraschiv dev_err(&pdev->dev, "Error in misc dev register [rc=%d]\n", rc);
530bd47c995SAndra Paraschiv
531bd47c995SAndra Paraschiv goto disable_ne_pci_dev;
532bd47c995SAndra Paraschiv }
533bd47c995SAndra Paraschiv
53489308c11SAndra Paraschiv return 0;
53589308c11SAndra Paraschiv
536bd47c995SAndra Paraschiv disable_ne_pci_dev:
537bd47c995SAndra Paraschiv ne_devs.ne_pci_dev = NULL;
538bd47c995SAndra Paraschiv ne_pci_dev_disable(pdev);
53989308c11SAndra Paraschiv teardown_msix:
54089308c11SAndra Paraschiv ne_teardown_msix(pdev);
54189308c11SAndra Paraschiv iounmap_pci_bar:
54289308c11SAndra Paraschiv pci_set_drvdata(pdev, NULL);
54389308c11SAndra Paraschiv pci_iounmap(pdev, ne_pci_dev->iomem_base);
54489308c11SAndra Paraschiv release_pci_regions:
54589308c11SAndra Paraschiv pci_release_regions(pdev);
54689308c11SAndra Paraschiv disable_pci_dev:
54789308c11SAndra Paraschiv pci_disable_device(pdev);
54889308c11SAndra Paraschiv free_ne_pci_dev:
54989308c11SAndra Paraschiv kfree(ne_pci_dev);
55089308c11SAndra Paraschiv
55189308c11SAndra Paraschiv return rc;
55289308c11SAndra Paraschiv }
55389308c11SAndra Paraschiv
55489308c11SAndra Paraschiv /**
55589308c11SAndra Paraschiv * ne_pci_remove() - Remove function for the NE PCI device.
55689308c11SAndra Paraschiv * @pdev: PCI device associated with the NE PCI driver.
55789308c11SAndra Paraschiv *
55889308c11SAndra Paraschiv * Context: Process context.
55989308c11SAndra Paraschiv */
ne_pci_remove(struct pci_dev * pdev)56089308c11SAndra Paraschiv static void ne_pci_remove(struct pci_dev *pdev)
56189308c11SAndra Paraschiv {
56289308c11SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
56389308c11SAndra Paraschiv
564bd47c995SAndra Paraschiv misc_deregister(ne_devs.ne_misc_dev);
565bd47c995SAndra Paraschiv
56689308c11SAndra Paraschiv ne_devs.ne_pci_dev = NULL;
56789308c11SAndra Paraschiv
56889308c11SAndra Paraschiv ne_pci_dev_disable(pdev);
56989308c11SAndra Paraschiv
57089308c11SAndra Paraschiv ne_teardown_msix(pdev);
57189308c11SAndra Paraschiv
57289308c11SAndra Paraschiv pci_set_drvdata(pdev, NULL);
57389308c11SAndra Paraschiv
57489308c11SAndra Paraschiv pci_iounmap(pdev, ne_pci_dev->iomem_base);
57589308c11SAndra Paraschiv
57689308c11SAndra Paraschiv pci_release_regions(pdev);
57789308c11SAndra Paraschiv
57889308c11SAndra Paraschiv pci_disable_device(pdev);
57989308c11SAndra Paraschiv
58089308c11SAndra Paraschiv kfree(ne_pci_dev);
58189308c11SAndra Paraschiv }
58289308c11SAndra Paraschiv
58389308c11SAndra Paraschiv /**
58489308c11SAndra Paraschiv * ne_pci_shutdown() - Shutdown function for the NE PCI device.
58589308c11SAndra Paraschiv * @pdev: PCI device associated with the NE PCI driver.
58689308c11SAndra Paraschiv *
58789308c11SAndra Paraschiv * Context: Process context.
58889308c11SAndra Paraschiv */
ne_pci_shutdown(struct pci_dev * pdev)58989308c11SAndra Paraschiv static void ne_pci_shutdown(struct pci_dev *pdev)
59089308c11SAndra Paraschiv {
59189308c11SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
59289308c11SAndra Paraschiv
59389308c11SAndra Paraschiv if (!ne_pci_dev)
59489308c11SAndra Paraschiv return;
59589308c11SAndra Paraschiv
596bd47c995SAndra Paraschiv misc_deregister(ne_devs.ne_misc_dev);
597bd47c995SAndra Paraschiv
59889308c11SAndra Paraschiv ne_devs.ne_pci_dev = NULL;
59989308c11SAndra Paraschiv
60089308c11SAndra Paraschiv ne_pci_dev_disable(pdev);
60189308c11SAndra Paraschiv
60289308c11SAndra Paraschiv ne_teardown_msix(pdev);
60389308c11SAndra Paraschiv
60489308c11SAndra Paraschiv pci_set_drvdata(pdev, NULL);
60589308c11SAndra Paraschiv
60689308c11SAndra Paraschiv pci_iounmap(pdev, ne_pci_dev->iomem_base);
60789308c11SAndra Paraschiv
60889308c11SAndra Paraschiv pci_release_regions(pdev);
60989308c11SAndra Paraschiv
61089308c11SAndra Paraschiv pci_disable_device(pdev);
61189308c11SAndra Paraschiv
61289308c11SAndra Paraschiv kfree(ne_pci_dev);
61389308c11SAndra Paraschiv }
61489308c11SAndra Paraschiv
61589308c11SAndra Paraschiv /*
61689308c11SAndra Paraschiv * TODO: Add suspend / resume functions for power management w/ CONFIG_PM, if
61789308c11SAndra Paraschiv * needed.
61889308c11SAndra Paraschiv */
61989308c11SAndra Paraschiv /* NE PCI device driver. */
62089308c11SAndra Paraschiv struct pci_driver ne_pci_driver = {
62189308c11SAndra Paraschiv .name = "nitro_enclaves",
62289308c11SAndra Paraschiv .id_table = ne_pci_ids,
62389308c11SAndra Paraschiv .probe = ne_pci_probe,
62489308c11SAndra Paraschiv .remove = ne_pci_remove,
62589308c11SAndra Paraschiv .shutdown = ne_pci_shutdown,
62689308c11SAndra Paraschiv };
627