blob: 35fbfbd73a6d835855b9d24af2e85e73fe5010a1 [file] [log] [blame]
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +05301/**
2 * Host side test driver to test endpoint functionality
3 *
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/crc32.h>
21#include <linux/delay.h>
22#include <linux/fs.h>
23#include <linux/io.h>
24#include <linux/interrupt.h>
25#include <linux/irq.h>
26#include <linux/miscdevice.h>
27#include <linux/module.h>
28#include <linux/mutex.h>
29#include <linux/random.h>
30#include <linux/slab.h>
31#include <linux/pci.h>
32#include <linux/pci_ids.h>
33
34#include <linux/pci_regs.h>
35
36#include <uapi/linux/pcitest.h>
37
Gustavo Pimentele8817de2018-07-19 10:32:17 +020038#define DRV_MODULE_NAME "pci-endpoint-test"
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +053039
Gustavo Pimentele8817de2018-07-19 10:32:17 +020040#define IRQ_TYPE_LEGACY 0
41#define IRQ_TYPE_MSI 1
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +053042
Gustavo Pimentele8817de2018-07-19 10:32:17 +020043#define PCI_ENDPOINT_TEST_MAGIC 0x0
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +053044
Gustavo Pimentele8817de2018-07-19 10:32:17 +020045#define PCI_ENDPOINT_TEST_COMMAND 0x4
46#define COMMAND_RAISE_LEGACY_IRQ BIT(0)
47#define COMMAND_RAISE_MSI_IRQ BIT(1)
48/* BIT(2) is reserved for raising MSI-X IRQ command */
49#define COMMAND_READ BIT(3)
50#define COMMAND_WRITE BIT(4)
51#define COMMAND_COPY BIT(5)
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +053052
Gustavo Pimentele8817de2018-07-19 10:32:17 +020053#define PCI_ENDPOINT_TEST_STATUS 0x8
54#define STATUS_READ_SUCCESS BIT(0)
55#define STATUS_READ_FAIL BIT(1)
56#define STATUS_WRITE_SUCCESS BIT(2)
57#define STATUS_WRITE_FAIL BIT(3)
58#define STATUS_COPY_SUCCESS BIT(4)
59#define STATUS_COPY_FAIL BIT(5)
60#define STATUS_IRQ_RAISED BIT(6)
61#define STATUS_SRC_ADDR_INVALID BIT(7)
62#define STATUS_DST_ADDR_INVALID BIT(8)
63
64#define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0x0c
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +053065#define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
66
67#define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
68#define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
69
Gustavo Pimentele8817de2018-07-19 10:32:17 +020070#define PCI_ENDPOINT_TEST_SIZE 0x1c
71#define PCI_ENDPOINT_TEST_CHECKSUM 0x20
72
73#define PCI_ENDPOINT_TEST_IRQ_TYPE 0x24
74#define PCI_ENDPOINT_TEST_IRQ_NUMBER 0x28
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +053075
76static DEFINE_IDA(pci_endpoint_test_ida);
77
78#define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
79 miscdev)
Kishon Vijay Abraham I0c8a5f92017-08-18 20:28:09 +053080
81static bool no_msi;
82module_param(no_msi, bool, 0444);
83MODULE_PARM_DESC(no_msi, "Disable MSI interrupt in pci_endpoint_test");
84
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +053085enum pci_barno {
86 BAR_0,
87 BAR_1,
88 BAR_2,
89 BAR_3,
90 BAR_4,
91 BAR_5,
92};
93
94struct pci_endpoint_test {
95 struct pci_dev *pdev;
96 void __iomem *base;
97 void __iomem *bar[6];
98 struct completion irq_raised;
99 int last_irq;
Kishon Vijay Abraham Ib7636e82017-10-11 14:14:38 +0530100 int num_irqs;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530101 /* mutex to protect the ioctls */
102 struct mutex mutex;
103 struct miscdevice miscdev;
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530104 enum pci_barno test_reg_bar;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530105 size_t alignment;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530106};
107
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530108struct pci_endpoint_test_data {
109 enum pci_barno test_reg_bar;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530110 size_t alignment;
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530111 bool no_msi;
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530112};
113
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530114static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
115 u32 offset)
116{
117 return readl(test->base + offset);
118}
119
120static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
121 u32 offset, u32 value)
122{
123 writel(value, test->base + offset);
124}
125
126static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
127 int bar, int offset)
128{
129 return readl(test->bar[bar] + offset);
130}
131
132static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
133 int bar, u32 offset, u32 value)
134{
135 writel(value, test->bar[bar] + offset);
136}
137
138static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
139{
140 struct pci_endpoint_test *test = dev_id;
141 u32 reg;
142
143 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
144 if (reg & STATUS_IRQ_RAISED) {
145 test->last_irq = irq;
146 complete(&test->irq_raised);
147 reg &= ~STATUS_IRQ_RAISED;
148 }
149 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS,
150 reg);
151
152 return IRQ_HANDLED;
153}
154
155static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
156 enum pci_barno barno)
157{
158 int j;
159 u32 val;
160 int size;
Kishon Vijay Abraham Icda370e2017-08-18 20:28:08 +0530161 struct pci_dev *pdev = test->pdev;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530162
163 if (!test->bar[barno])
164 return false;
165
Kishon Vijay Abraham Icda370e2017-08-18 20:28:08 +0530166 size = pci_resource_len(pdev, barno);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530167
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530168 if (barno == test->test_reg_bar)
169 size = 0x4;
170
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530171 for (j = 0; j < size; j += 4)
172 pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0);
173
174 for (j = 0; j < size; j += 4) {
175 val = pci_endpoint_test_bar_readl(test, barno, j);
176 if (val != 0xA0A0A0A0)
177 return false;
178 }
179
180 return true;
181}
182
183static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test)
184{
185 u32 val;
186
Gustavo Pimentele8817de2018-07-19 10:32:17 +0200187 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
188 IRQ_TYPE_LEGACY);
189 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 0);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530190 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
191 COMMAND_RAISE_LEGACY_IRQ);
192 val = wait_for_completion_timeout(&test->irq_raised,
193 msecs_to_jiffies(1000));
194 if (!val)
195 return false;
196
197 return true;
198}
199
200static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
201 u8 msi_num)
202{
203 u32 val;
204 struct pci_dev *pdev = test->pdev;
205
Gustavo Pimentele8817de2018-07-19 10:32:17 +0200206 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
207 IRQ_TYPE_MSI);
208 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, msi_num);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530209 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530210 COMMAND_RAISE_MSI_IRQ);
211 val = wait_for_completion_timeout(&test->irq_raised,
212 msecs_to_jiffies(1000));
213 if (!val)
214 return false;
215
Gustavo Pimentelecc57ef2018-05-14 18:27:48 +0100216 if (pci_irq_vector(pdev, msi_num - 1) == test->last_irq)
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530217 return true;
218
219 return false;
220}
221
222static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
223{
224 bool ret = false;
225 void *src_addr;
226 void *dst_addr;
227 dma_addr_t src_phys_addr;
228 dma_addr_t dst_phys_addr;
229 struct pci_dev *pdev = test->pdev;
230 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530231 void *orig_src_addr;
232 dma_addr_t orig_src_phys_addr;
233 void *orig_dst_addr;
234 dma_addr_t orig_dst_phys_addr;
235 size_t offset;
236 size_t alignment = test->alignment;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530237 u32 src_crc32;
238 u32 dst_crc32;
239
Dan Carpenter343dc692017-09-30 11:15:52 +0300240 if (size > SIZE_MAX - alignment)
241 goto err;
242
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530243 orig_src_addr = dma_alloc_coherent(dev, size + alignment,
244 &orig_src_phys_addr, GFP_KERNEL);
245 if (!orig_src_addr) {
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100246 dev_err(dev, "Failed to allocate source buffer\n");
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530247 ret = false;
248 goto err;
249 }
250
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530251 if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
252 src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
253 offset = src_phys_addr - orig_src_phys_addr;
254 src_addr = orig_src_addr + offset;
255 } else {
256 src_phys_addr = orig_src_phys_addr;
257 src_addr = orig_src_addr;
258 }
259
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530260 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
261 lower_32_bits(src_phys_addr));
262
263 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
264 upper_32_bits(src_phys_addr));
265
266 get_random_bytes(src_addr, size);
267 src_crc32 = crc32_le(~0, src_addr, size);
268
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530269 orig_dst_addr = dma_alloc_coherent(dev, size + alignment,
270 &orig_dst_phys_addr, GFP_KERNEL);
271 if (!orig_dst_addr) {
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100272 dev_err(dev, "Failed to allocate destination address\n");
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530273 ret = false;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530274 goto err_orig_src_addr;
275 }
276
277 if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
278 dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
279 offset = dst_phys_addr - orig_dst_phys_addr;
280 dst_addr = orig_dst_addr + offset;
281 } else {
282 dst_phys_addr = orig_dst_phys_addr;
283 dst_addr = orig_dst_addr;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530284 }
285
286 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
287 lower_32_bits(dst_phys_addr));
288 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
289 upper_32_bits(dst_phys_addr));
290
291 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
292 size);
293
Gustavo Pimentele8817de2018-07-19 10:32:17 +0200294 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
295 no_msi ? IRQ_TYPE_LEGACY : IRQ_TYPE_MSI);
296 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530297 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
Gustavo Pimentele8817de2018-07-19 10:32:17 +0200298 COMMAND_COPY);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530299
300 wait_for_completion(&test->irq_raised);
301
302 dst_crc32 = crc32_le(~0, dst_addr, size);
303 if (dst_crc32 == src_crc32)
304 ret = true;
305
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530306 dma_free_coherent(dev, size + alignment, orig_dst_addr,
307 orig_dst_phys_addr);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530308
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530309err_orig_src_addr:
310 dma_free_coherent(dev, size + alignment, orig_src_addr,
311 orig_src_phys_addr);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530312
313err:
314 return ret;
315}
316
317static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
318{
319 bool ret = false;
320 u32 reg;
321 void *addr;
322 dma_addr_t phys_addr;
323 struct pci_dev *pdev = test->pdev;
324 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530325 void *orig_addr;
326 dma_addr_t orig_phys_addr;
327 size_t offset;
328 size_t alignment = test->alignment;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530329 u32 crc32;
330
Dan Carpenter343dc692017-09-30 11:15:52 +0300331 if (size > SIZE_MAX - alignment)
332 goto err;
333
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530334 orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
335 GFP_KERNEL);
336 if (!orig_addr) {
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100337 dev_err(dev, "Failed to allocate address\n");
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530338 ret = false;
339 goto err;
340 }
341
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530342 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
343 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
344 offset = phys_addr - orig_phys_addr;
345 addr = orig_addr + offset;
346 } else {
347 phys_addr = orig_phys_addr;
348 addr = orig_addr;
349 }
350
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530351 get_random_bytes(addr, size);
352
353 crc32 = crc32_le(~0, addr, size);
354 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
355 crc32);
356
357 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
358 lower_32_bits(phys_addr));
359 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
360 upper_32_bits(phys_addr));
361
362 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
363
Gustavo Pimentele8817de2018-07-19 10:32:17 +0200364 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
365 no_msi ? IRQ_TYPE_LEGACY : IRQ_TYPE_MSI);
366 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530367 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
Gustavo Pimentele8817de2018-07-19 10:32:17 +0200368 COMMAND_READ);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530369
370 wait_for_completion(&test->irq_raised);
371
372 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
373 if (reg & STATUS_READ_SUCCESS)
374 ret = true;
375
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530376 dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530377
378err:
379 return ret;
380}
381
382static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
383{
384 bool ret = false;
385 void *addr;
386 dma_addr_t phys_addr;
387 struct pci_dev *pdev = test->pdev;
388 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530389 void *orig_addr;
390 dma_addr_t orig_phys_addr;
391 size_t offset;
392 size_t alignment = test->alignment;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530393 u32 crc32;
394
Dan Carpenter343dc692017-09-30 11:15:52 +0300395 if (size > SIZE_MAX - alignment)
396 goto err;
397
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530398 orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
399 GFP_KERNEL);
400 if (!orig_addr) {
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100401 dev_err(dev, "Failed to allocate destination address\n");
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530402 ret = false;
403 goto err;
404 }
405
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530406 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
407 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
408 offset = phys_addr - orig_phys_addr;
409 addr = orig_addr + offset;
410 } else {
411 phys_addr = orig_phys_addr;
412 addr = orig_addr;
413 }
414
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530415 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
416 lower_32_bits(phys_addr));
417 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
418 upper_32_bits(phys_addr));
419
420 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
421
Gustavo Pimentele8817de2018-07-19 10:32:17 +0200422 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
423 no_msi ? IRQ_TYPE_LEGACY : IRQ_TYPE_MSI);
424 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530425 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
Gustavo Pimentele8817de2018-07-19 10:32:17 +0200426 COMMAND_WRITE);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530427
428 wait_for_completion(&test->irq_raised);
429
430 crc32 = crc32_le(~0, addr, size);
431 if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
432 ret = true;
433
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530434 dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530435err:
436 return ret;
437}
438
439static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
440 unsigned long arg)
441{
442 int ret = -EINVAL;
443 enum pci_barno bar;
444 struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
445
446 mutex_lock(&test->mutex);
447 switch (cmd) {
448 case PCITEST_BAR:
449 bar = arg;
450 if (bar < 0 || bar > 5)
451 goto ret;
452 ret = pci_endpoint_test_bar(test, bar);
453 break;
454 case PCITEST_LEGACY_IRQ:
455 ret = pci_endpoint_test_legacy_irq(test);
456 break;
457 case PCITEST_MSI:
458 ret = pci_endpoint_test_msi_irq(test, arg);
459 break;
460 case PCITEST_WRITE:
461 ret = pci_endpoint_test_write(test, arg);
462 break;
463 case PCITEST_READ:
464 ret = pci_endpoint_test_read(test, arg);
465 break;
466 case PCITEST_COPY:
467 ret = pci_endpoint_test_copy(test, arg);
468 break;
469 }
470
471ret:
472 mutex_unlock(&test->mutex);
473 return ret;
474}
475
476static const struct file_operations pci_endpoint_test_fops = {
477 .owner = THIS_MODULE,
478 .unlocked_ioctl = pci_endpoint_test_ioctl,
479};
480
481static int pci_endpoint_test_probe(struct pci_dev *pdev,
482 const struct pci_device_id *ent)
483{
484 int i;
485 int err;
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530486 int irq = 0;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530487 int id;
488 char name[20];
489 enum pci_barno bar;
490 void __iomem *base;
491 struct device *dev = &pdev->dev;
492 struct pci_endpoint_test *test;
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530493 struct pci_endpoint_test_data *data;
494 enum pci_barno test_reg_bar = BAR_0;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530495 struct miscdevice *misc_device;
496
497 if (pci_is_bridge(pdev))
498 return -ENODEV;
499
500 test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
501 if (!test)
502 return -ENOMEM;
503
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530504 test->test_reg_bar = 0;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530505 test->alignment = 0;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530506 test->pdev = pdev;
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530507
508 data = (struct pci_endpoint_test_data *)ent->driver_data;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530509 if (data) {
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530510 test_reg_bar = data->test_reg_bar;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530511 test->alignment = data->alignment;
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530512 no_msi = data->no_msi;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530513 }
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530514
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530515 init_completion(&test->irq_raised);
516 mutex_init(&test->mutex);
517
518 err = pci_enable_device(pdev);
519 if (err) {
520 dev_err(dev, "Cannot enable PCI device\n");
521 return err;
522 }
523
524 err = pci_request_regions(pdev, DRV_MODULE_NAME);
525 if (err) {
526 dev_err(dev, "Cannot obtain PCI resources\n");
527 goto err_disable_pdev;
528 }
529
530 pci_set_master(pdev);
531
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530532 if (!no_msi) {
533 irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
534 if (irq < 0)
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100535 dev_err(dev, "Failed to get MSI interrupts\n");
Kishon Vijay Abraham Ib7636e82017-10-11 14:14:38 +0530536 test->num_irqs = irq;
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530537 }
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530538
539 err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler,
540 IRQF_SHARED, DRV_MODULE_NAME, test);
541 if (err) {
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100542 dev_err(dev, "Failed to request IRQ %d\n", pdev->irq);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530543 goto err_disable_msi;
544 }
545
546 for (i = 1; i < irq; i++) {
Gustavo Pimentelecc57ef2018-05-14 18:27:48 +0100547 err = devm_request_irq(dev, pci_irq_vector(pdev, i),
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530548 pci_endpoint_test_irqhandler,
549 IRQF_SHARED, DRV_MODULE_NAME, test);
550 if (err)
551 dev_err(dev, "failed to request IRQ %d for MSI %d\n",
Gustavo Pimentelecc57ef2018-05-14 18:27:48 +0100552 pci_irq_vector(pdev, i), i + 1);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530553 }
554
555 for (bar = BAR_0; bar <= BAR_5; bar++) {
Niklas Cassel16b17ca2018-03-28 13:50:17 +0200556 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
557 base = pci_ioremap_bar(pdev, bar);
558 if (!base) {
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100559 dev_err(dev, "Failed to read BAR%d\n", bar);
Niklas Cassel16b17ca2018-03-28 13:50:17 +0200560 WARN_ON(bar == test_reg_bar);
561 }
562 test->bar[bar] = base;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530563 }
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530564 }
565
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530566 test->base = test->bar[test_reg_bar];
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530567 if (!test->base) {
Kishon Vijay Abraham I80068c92017-10-11 14:14:36 +0530568 err = -ENOMEM;
Kishon Vijay Abraham I834b9052017-08-18 20:28:05 +0530569 dev_err(dev, "Cannot perform PCI test without BAR%d\n",
570 test_reg_bar);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530571 goto err_iounmap;
572 }
573
574 pci_set_drvdata(pdev, test);
575
576 id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
577 if (id < 0) {
Kishon Vijay Abraham I80068c92017-10-11 14:14:36 +0530578 err = id;
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100579 dev_err(dev, "Unable to get id\n");
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530580 goto err_iounmap;
581 }
582
583 snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
584 misc_device = &test->miscdev;
585 misc_device->minor = MISC_DYNAMIC_MINOR;
Kishon Vijay Abraham I139838f2017-10-11 14:14:37 +0530586 misc_device->name = kstrdup(name, GFP_KERNEL);
587 if (!misc_device->name) {
588 err = -ENOMEM;
589 goto err_ida_remove;
590 }
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530591 misc_device->fops = &pci_endpoint_test_fops,
592
593 err = misc_register(misc_device);
594 if (err) {
Gustavo Pimentel0e52ea62018-05-14 17:56:23 +0100595 dev_err(dev, "Failed to register device\n");
Kishon Vijay Abraham I139838f2017-10-11 14:14:37 +0530596 goto err_kfree_name;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530597 }
598
599 return 0;
600
Kishon Vijay Abraham I139838f2017-10-11 14:14:37 +0530601err_kfree_name:
602 kfree(misc_device->name);
603
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530604err_ida_remove:
605 ida_simple_remove(&pci_endpoint_test_ida, id);
606
607err_iounmap:
608 for (bar = BAR_0; bar <= BAR_5; bar++) {
609 if (test->bar[bar])
610 pci_iounmap(pdev, test->bar[bar]);
611 }
612
Kishon Vijay Abraham Ib7636e82017-10-11 14:14:38 +0530613 for (i = 0; i < irq; i++)
Gustavo Pimentelecc57ef2018-05-14 18:27:48 +0100614 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), test);
Kishon Vijay Abraham Ib7636e82017-10-11 14:14:38 +0530615
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530616err_disable_msi:
617 pci_disable_msi(pdev);
618 pci_release_regions(pdev);
619
620err_disable_pdev:
621 pci_disable_device(pdev);
622
623 return err;
624}
625
626static void pci_endpoint_test_remove(struct pci_dev *pdev)
627{
628 int id;
Kishon Vijay Abraham Ib7636e82017-10-11 14:14:38 +0530629 int i;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530630 enum pci_barno bar;
631 struct pci_endpoint_test *test = pci_get_drvdata(pdev);
632 struct miscdevice *misc_device = &test->miscdev;
633
634 if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
635 return;
Dan Carpentera2db2662017-09-30 11:16:51 +0300636 if (id < 0)
637 return;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530638
639 misc_deregister(&test->miscdev);
Kishon Vijay Abraham I139838f2017-10-11 14:14:37 +0530640 kfree(misc_device->name);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530641 ida_simple_remove(&pci_endpoint_test_ida, id);
642 for (bar = BAR_0; bar <= BAR_5; bar++) {
643 if (test->bar[bar])
644 pci_iounmap(pdev, test->bar[bar]);
645 }
Kishon Vijay Abraham Ib7636e82017-10-11 14:14:38 +0530646 for (i = 0; i < test->num_irqs; i++)
Gustavo Pimentelecc57ef2018-05-14 18:27:48 +0100647 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), test);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530648 pci_disable_msi(pdev);
649 pci_release_regions(pdev);
650 pci_disable_device(pdev);
651}
652
653static const struct pci_device_id pci_endpoint_test_tbl[] = {
654 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
655 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
Gustavo Pimentel14b06dd2018-05-15 15:41:44 +0100656 { PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS, 0xedda) },
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530657 { }
658};
659MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
660
661static struct pci_driver pci_endpoint_test_driver = {
662 .name = DRV_MODULE_NAME,
663 .id_table = pci_endpoint_test_tbl,
664 .probe = pci_endpoint_test_probe,
665 .remove = pci_endpoint_test_remove,
666};
667module_pci_driver(pci_endpoint_test_driver);
668
669MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
670MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
671MODULE_LICENSE("GPL v2");