blob: 1f37ad39b16929cec0fe7aa9e082de5bf12b6931 [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
38#define DRV_MODULE_NAME "pci-endpoint-test"
39
40#define PCI_ENDPOINT_TEST_MAGIC 0x0
41
42#define PCI_ENDPOINT_TEST_COMMAND 0x4
43#define COMMAND_RAISE_LEGACY_IRQ BIT(0)
44#define COMMAND_RAISE_MSI_IRQ BIT(1)
45#define MSI_NUMBER_SHIFT 2
46/* 6 bits for MSI number */
47#define COMMAND_READ BIT(8)
48#define COMMAND_WRITE BIT(9)
49#define COMMAND_COPY BIT(10)
50
51#define PCI_ENDPOINT_TEST_STATUS 0x8
52#define STATUS_READ_SUCCESS BIT(0)
53#define STATUS_READ_FAIL BIT(1)
54#define STATUS_WRITE_SUCCESS BIT(2)
55#define STATUS_WRITE_FAIL BIT(3)
56#define STATUS_COPY_SUCCESS BIT(4)
57#define STATUS_COPY_FAIL BIT(5)
58#define STATUS_IRQ_RAISED BIT(6)
59#define STATUS_SRC_ADDR_INVALID BIT(7)
60#define STATUS_DST_ADDR_INVALID BIT(8)
61
62#define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0xc
63#define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
64
65#define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
66#define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
67
68#define PCI_ENDPOINT_TEST_SIZE 0x1c
69#define PCI_ENDPOINT_TEST_CHECKSUM 0x20
70
71static DEFINE_IDA(pci_endpoint_test_ida);
72
73#define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
74 miscdev)
75enum pci_barno {
76 BAR_0,
77 BAR_1,
78 BAR_2,
79 BAR_3,
80 BAR_4,
81 BAR_5,
82};
83
84struct pci_endpoint_test {
85 struct pci_dev *pdev;
86 void __iomem *base;
87 void __iomem *bar[6];
88 struct completion irq_raised;
89 int last_irq;
90 /* mutex to protect the ioctls */
91 struct mutex mutex;
92 struct miscdevice miscdev;
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +053093 enum pci_barno test_reg_bar;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +053094 size_t alignment;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +053095};
96
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +053097struct pci_endpoint_test_data {
98 enum pci_barno test_reg_bar;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +053099 size_t alignment;
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530100 bool no_msi;
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530101};
102
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530103static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
104 u32 offset)
105{
106 return readl(test->base + offset);
107}
108
109static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
110 u32 offset, u32 value)
111{
112 writel(value, test->base + offset);
113}
114
115static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
116 int bar, int offset)
117{
118 return readl(test->bar[bar] + offset);
119}
120
121static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
122 int bar, u32 offset, u32 value)
123{
124 writel(value, test->bar[bar] + offset);
125}
126
127static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
128{
129 struct pci_endpoint_test *test = dev_id;
130 u32 reg;
131
132 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
133 if (reg & STATUS_IRQ_RAISED) {
134 test->last_irq = irq;
135 complete(&test->irq_raised);
136 reg &= ~STATUS_IRQ_RAISED;
137 }
138 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS,
139 reg);
140
141 return IRQ_HANDLED;
142}
143
144static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
145 enum pci_barno barno)
146{
147 int j;
148 u32 val;
149 int size;
Kishon Vijay Abraham Icda370e2017-08-18 20:28:08 +0530150 struct pci_dev *pdev = test->pdev;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530151
152 if (!test->bar[barno])
153 return false;
154
Kishon Vijay Abraham Icda370e2017-08-18 20:28:08 +0530155 size = pci_resource_len(pdev, barno);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530156
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530157 if (barno == test->test_reg_bar)
158 size = 0x4;
159
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530160 for (j = 0; j < size; j += 4)
161 pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0);
162
163 for (j = 0; j < size; j += 4) {
164 val = pci_endpoint_test_bar_readl(test, barno, j);
165 if (val != 0xA0A0A0A0)
166 return false;
167 }
168
169 return true;
170}
171
172static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test)
173{
174 u32 val;
175
176 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
177 COMMAND_RAISE_LEGACY_IRQ);
178 val = wait_for_completion_timeout(&test->irq_raised,
179 msecs_to_jiffies(1000));
180 if (!val)
181 return false;
182
183 return true;
184}
185
186static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
187 u8 msi_num)
188{
189 u32 val;
190 struct pci_dev *pdev = test->pdev;
191
192 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
193 msi_num << MSI_NUMBER_SHIFT |
194 COMMAND_RAISE_MSI_IRQ);
195 val = wait_for_completion_timeout(&test->irq_raised,
196 msecs_to_jiffies(1000));
197 if (!val)
198 return false;
199
200 if (test->last_irq - pdev->irq == msi_num - 1)
201 return true;
202
203 return false;
204}
205
206static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
207{
208 bool ret = false;
209 void *src_addr;
210 void *dst_addr;
211 dma_addr_t src_phys_addr;
212 dma_addr_t dst_phys_addr;
213 struct pci_dev *pdev = test->pdev;
214 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530215 void *orig_src_addr;
216 dma_addr_t orig_src_phys_addr;
217 void *orig_dst_addr;
218 dma_addr_t orig_dst_phys_addr;
219 size_t offset;
220 size_t alignment = test->alignment;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530221 u32 src_crc32;
222 u32 dst_crc32;
223
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530224 orig_src_addr = dma_alloc_coherent(dev, size + alignment,
225 &orig_src_phys_addr, GFP_KERNEL);
226 if (!orig_src_addr) {
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530227 dev_err(dev, "failed to allocate source buffer\n");
228 ret = false;
229 goto err;
230 }
231
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530232 if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
233 src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
234 offset = src_phys_addr - orig_src_phys_addr;
235 src_addr = orig_src_addr + offset;
236 } else {
237 src_phys_addr = orig_src_phys_addr;
238 src_addr = orig_src_addr;
239 }
240
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530241 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
242 lower_32_bits(src_phys_addr));
243
244 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
245 upper_32_bits(src_phys_addr));
246
247 get_random_bytes(src_addr, size);
248 src_crc32 = crc32_le(~0, src_addr, size);
249
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530250 orig_dst_addr = dma_alloc_coherent(dev, size + alignment,
251 &orig_dst_phys_addr, GFP_KERNEL);
252 if (!orig_dst_addr) {
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530253 dev_err(dev, "failed to allocate destination address\n");
254 ret = false;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530255 goto err_orig_src_addr;
256 }
257
258 if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
259 dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
260 offset = dst_phys_addr - orig_dst_phys_addr;
261 dst_addr = orig_dst_addr + offset;
262 } else {
263 dst_phys_addr = orig_dst_phys_addr;
264 dst_addr = orig_dst_addr;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530265 }
266
267 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
268 lower_32_bits(dst_phys_addr));
269 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
270 upper_32_bits(dst_phys_addr));
271
272 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
273 size);
274
275 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
276 1 << MSI_NUMBER_SHIFT | COMMAND_COPY);
277
278 wait_for_completion(&test->irq_raised);
279
280 dst_crc32 = crc32_le(~0, dst_addr, size);
281 if (dst_crc32 == src_crc32)
282 ret = true;
283
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530284 dma_free_coherent(dev, size + alignment, orig_dst_addr,
285 orig_dst_phys_addr);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530286
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530287err_orig_src_addr:
288 dma_free_coherent(dev, size + alignment, orig_src_addr,
289 orig_src_phys_addr);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530290
291err:
292 return ret;
293}
294
295static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
296{
297 bool ret = false;
298 u32 reg;
299 void *addr;
300 dma_addr_t phys_addr;
301 struct pci_dev *pdev = test->pdev;
302 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530303 void *orig_addr;
304 dma_addr_t orig_phys_addr;
305 size_t offset;
306 size_t alignment = test->alignment;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530307 u32 crc32;
308
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530309 orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
310 GFP_KERNEL);
311 if (!orig_addr) {
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530312 dev_err(dev, "failed to allocate address\n");
313 ret = false;
314 goto err;
315 }
316
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530317 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
318 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
319 offset = phys_addr - orig_phys_addr;
320 addr = orig_addr + offset;
321 } else {
322 phys_addr = orig_phys_addr;
323 addr = orig_addr;
324 }
325
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530326 get_random_bytes(addr, size);
327
328 crc32 = crc32_le(~0, addr, size);
329 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
330 crc32);
331
332 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
333 lower_32_bits(phys_addr));
334 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
335 upper_32_bits(phys_addr));
336
337 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
338
339 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
340 1 << MSI_NUMBER_SHIFT | COMMAND_READ);
341
342 wait_for_completion(&test->irq_raised);
343
344 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
345 if (reg & STATUS_READ_SUCCESS)
346 ret = true;
347
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530348 dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530349
350err:
351 return ret;
352}
353
354static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
355{
356 bool ret = false;
357 void *addr;
358 dma_addr_t phys_addr;
359 struct pci_dev *pdev = test->pdev;
360 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530361 void *orig_addr;
362 dma_addr_t orig_phys_addr;
363 size_t offset;
364 size_t alignment = test->alignment;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530365 u32 crc32;
366
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530367 orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
368 GFP_KERNEL);
369 if (!orig_addr) {
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530370 dev_err(dev, "failed to allocate destination address\n");
371 ret = false;
372 goto err;
373 }
374
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530375 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
376 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
377 offset = phys_addr - orig_phys_addr;
378 addr = orig_addr + offset;
379 } else {
380 phys_addr = orig_phys_addr;
381 addr = orig_addr;
382 }
383
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530384 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
385 lower_32_bits(phys_addr));
386 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
387 upper_32_bits(phys_addr));
388
389 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
390
391 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
392 1 << MSI_NUMBER_SHIFT | COMMAND_WRITE);
393
394 wait_for_completion(&test->irq_raised);
395
396 crc32 = crc32_le(~0, addr, size);
397 if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
398 ret = true;
399
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530400 dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530401err:
402 return ret;
403}
404
405static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
406 unsigned long arg)
407{
408 int ret = -EINVAL;
409 enum pci_barno bar;
410 struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
411
412 mutex_lock(&test->mutex);
413 switch (cmd) {
414 case PCITEST_BAR:
415 bar = arg;
416 if (bar < 0 || bar > 5)
417 goto ret;
418 ret = pci_endpoint_test_bar(test, bar);
419 break;
420 case PCITEST_LEGACY_IRQ:
421 ret = pci_endpoint_test_legacy_irq(test);
422 break;
423 case PCITEST_MSI:
424 ret = pci_endpoint_test_msi_irq(test, arg);
425 break;
426 case PCITEST_WRITE:
427 ret = pci_endpoint_test_write(test, arg);
428 break;
429 case PCITEST_READ:
430 ret = pci_endpoint_test_read(test, arg);
431 break;
432 case PCITEST_COPY:
433 ret = pci_endpoint_test_copy(test, arg);
434 break;
435 }
436
437ret:
438 mutex_unlock(&test->mutex);
439 return ret;
440}
441
442static const struct file_operations pci_endpoint_test_fops = {
443 .owner = THIS_MODULE,
444 .unlocked_ioctl = pci_endpoint_test_ioctl,
445};
446
447static int pci_endpoint_test_probe(struct pci_dev *pdev,
448 const struct pci_device_id *ent)
449{
450 int i;
451 int err;
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530452 int irq = 0;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530453 int id;
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530454 bool no_msi = false;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530455 char name[20];
456 enum pci_barno bar;
457 void __iomem *base;
458 struct device *dev = &pdev->dev;
459 struct pci_endpoint_test *test;
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530460 struct pci_endpoint_test_data *data;
461 enum pci_barno test_reg_bar = BAR_0;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530462 struct miscdevice *misc_device;
463
464 if (pci_is_bridge(pdev))
465 return -ENODEV;
466
467 test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
468 if (!test)
469 return -ENOMEM;
470
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530471 test->test_reg_bar = 0;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530472 test->alignment = 0;
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530473 test->pdev = pdev;
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530474
475 data = (struct pci_endpoint_test_data *)ent->driver_data;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530476 if (data) {
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530477 test_reg_bar = data->test_reg_bar;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530478 test->alignment = data->alignment;
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530479 no_msi = data->no_msi;
Kishon Vijay Abraham I13107c62017-08-18 20:28:06 +0530480 }
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530481
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530482 init_completion(&test->irq_raised);
483 mutex_init(&test->mutex);
484
485 err = pci_enable_device(pdev);
486 if (err) {
487 dev_err(dev, "Cannot enable PCI device\n");
488 return err;
489 }
490
491 err = pci_request_regions(pdev, DRV_MODULE_NAME);
492 if (err) {
493 dev_err(dev, "Cannot obtain PCI resources\n");
494 goto err_disable_pdev;
495 }
496
497 pci_set_master(pdev);
498
Kishon Vijay Abraham I0b915162017-08-18 20:28:07 +0530499 if (!no_msi) {
500 irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
501 if (irq < 0)
502 dev_err(dev, "failed to get MSI interrupts\n");
503 }
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530504
505 err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler,
506 IRQF_SHARED, DRV_MODULE_NAME, test);
507 if (err) {
508 dev_err(dev, "failed to request IRQ %d\n", pdev->irq);
509 goto err_disable_msi;
510 }
511
512 for (i = 1; i < irq; i++) {
513 err = devm_request_irq(dev, pdev->irq + i,
514 pci_endpoint_test_irqhandler,
515 IRQF_SHARED, DRV_MODULE_NAME, test);
516 if (err)
517 dev_err(dev, "failed to request IRQ %d for MSI %d\n",
518 pdev->irq + i, i + 1);
519 }
520
521 for (bar = BAR_0; bar <= BAR_5; bar++) {
522 base = pci_ioremap_bar(pdev, bar);
523 if (!base) {
524 dev_err(dev, "failed to read BAR%d\n", bar);
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530525 WARN_ON(bar == test_reg_bar);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530526 }
527 test->bar[bar] = base;
528 }
529
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530530 test->base = test->bar[test_reg_bar];
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530531 if (!test->base) {
Kishon Vijay Abraham I834b90512017-08-18 20:28:05 +0530532 dev_err(dev, "Cannot perform PCI test without BAR%d\n",
533 test_reg_bar);
Kishon Vijay Abraham I2c156ac2017-03-27 15:15:14 +0530534 goto err_iounmap;
535 }
536
537 pci_set_drvdata(pdev, test);
538
539 id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
540 if (id < 0) {
541 dev_err(dev, "unable to get id\n");
542 goto err_iounmap;
543 }
544
545 snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
546 misc_device = &test->miscdev;
547 misc_device->minor = MISC_DYNAMIC_MINOR;
548 misc_device->name = name;
549 misc_device->fops = &pci_endpoint_test_fops,
550
551 err = misc_register(misc_device);
552 if (err) {
553 dev_err(dev, "failed to register device\n");
554 goto err_ida_remove;
555 }
556
557 return 0;
558
559err_ida_remove:
560 ida_simple_remove(&pci_endpoint_test_ida, id);
561
562err_iounmap:
563 for (bar = BAR_0; bar <= BAR_5; bar++) {
564 if (test->bar[bar])
565 pci_iounmap(pdev, test->bar[bar]);
566 }
567
568err_disable_msi:
569 pci_disable_msi(pdev);
570 pci_release_regions(pdev);
571
572err_disable_pdev:
573 pci_disable_device(pdev);
574
575 return err;
576}
577
578static void pci_endpoint_test_remove(struct pci_dev *pdev)
579{
580 int id;
581 enum pci_barno bar;
582 struct pci_endpoint_test *test = pci_get_drvdata(pdev);
583 struct miscdevice *misc_device = &test->miscdev;
584
585 if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
586 return;
587
588 misc_deregister(&test->miscdev);
589 ida_simple_remove(&pci_endpoint_test_ida, id);
590 for (bar = BAR_0; bar <= BAR_5; bar++) {
591 if (test->bar[bar])
592 pci_iounmap(pdev, test->bar[bar]);
593 }
594 pci_disable_msi(pdev);
595 pci_release_regions(pdev);
596 pci_disable_device(pdev);
597}
598
599static const struct pci_device_id pci_endpoint_test_tbl[] = {
600 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
601 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
602 { }
603};
604MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
605
606static struct pci_driver pci_endpoint_test_driver = {
607 .name = DRV_MODULE_NAME,
608 .id_table = pci_endpoint_test_tbl,
609 .probe = pci_endpoint_test_probe,
610 .remove = pci_endpoint_test_remove,
611};
612module_pci_driver(pci_endpoint_test_driver);
613
614MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
615MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
616MODULE_LICENSE("GPL v2");