PCI: endpoint: Add support to use _any_ BAR to map PCI_ENDPOINT_TEST regs
[linux-2.6-block.git] / drivers / pci / endpoint / functions / pci-epf-test.c
1 /**
2  * 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/io.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/pci_ids.h>
26 #include <linux/random.h>
27
28 #include <linux/pci-epc.h>
29 #include <linux/pci-epf.h>
30 #include <linux/pci_regs.h>
31
32 #define COMMAND_RAISE_LEGACY_IRQ        BIT(0)
33 #define COMMAND_RAISE_MSI_IRQ           BIT(1)
34 #define MSI_NUMBER_SHIFT                2
35 #define MSI_NUMBER_MASK                 (0x3f << MSI_NUMBER_SHIFT)
36 #define COMMAND_READ                    BIT(8)
37 #define COMMAND_WRITE                   BIT(9)
38 #define COMMAND_COPY                    BIT(10)
39
40 #define STATUS_READ_SUCCESS             BIT(0)
41 #define STATUS_READ_FAIL                BIT(1)
42 #define STATUS_WRITE_SUCCESS            BIT(2)
43 #define STATUS_WRITE_FAIL               BIT(3)
44 #define STATUS_COPY_SUCCESS             BIT(4)
45 #define STATUS_COPY_FAIL                BIT(5)
46 #define STATUS_IRQ_RAISED               BIT(6)
47 #define STATUS_SRC_ADDR_INVALID         BIT(7)
48 #define STATUS_DST_ADDR_INVALID         BIT(8)
49
50 #define TIMER_RESOLUTION                1
51
52 static struct workqueue_struct *kpcitest_workqueue;
53
54 struct pci_epf_test {
55         void                    *reg[6];
56         struct pci_epf          *epf;
57         enum pci_barno          test_reg_bar;
58         struct delayed_work     cmd_handler;
59 };
60
61 struct pci_epf_test_reg {
62         u32     magic;
63         u32     command;
64         u32     status;
65         u64     src_addr;
66         u64     dst_addr;
67         u32     size;
68         u32     checksum;
69 } __packed;
70
71 static struct pci_epf_header test_header = {
72         .vendorid       = PCI_ANY_ID,
73         .deviceid       = PCI_ANY_ID,
74         .baseclass_code = PCI_CLASS_OTHERS,
75         .interrupt_pin  = PCI_INTERRUPT_INTA,
76 };
77
78 struct pci_epf_test_data {
79         enum pci_barno  test_reg_bar;
80 };
81
82 static int bar_size[] = { 512, 512, 1024, 16384, 131072, 1048576 };
83
84 static int pci_epf_test_copy(struct pci_epf_test *epf_test)
85 {
86         int ret;
87         void __iomem *src_addr;
88         void __iomem *dst_addr;
89         phys_addr_t src_phys_addr;
90         phys_addr_t dst_phys_addr;
91         struct pci_epf *epf = epf_test->epf;
92         struct device *dev = &epf->dev;
93         struct pci_epc *epc = epf->epc;
94         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
95         struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
96
97         src_addr = pci_epc_mem_alloc_addr(epc, &src_phys_addr, reg->size);
98         if (!src_addr) {
99                 dev_err(dev, "failed to allocate source address\n");
100                 reg->status = STATUS_SRC_ADDR_INVALID;
101                 ret = -ENOMEM;
102                 goto err;
103         }
104
105         ret = pci_epc_map_addr(epc, src_phys_addr, reg->src_addr, reg->size);
106         if (ret) {
107                 dev_err(dev, "failed to map source address\n");
108                 reg->status = STATUS_SRC_ADDR_INVALID;
109                 goto err_src_addr;
110         }
111
112         dst_addr = pci_epc_mem_alloc_addr(epc, &dst_phys_addr, reg->size);
113         if (!dst_addr) {
114                 dev_err(dev, "failed to allocate destination address\n");
115                 reg->status = STATUS_DST_ADDR_INVALID;
116                 ret = -ENOMEM;
117                 goto err_src_map_addr;
118         }
119
120         ret = pci_epc_map_addr(epc, dst_phys_addr, reg->dst_addr, reg->size);
121         if (ret) {
122                 dev_err(dev, "failed to map destination address\n");
123                 reg->status = STATUS_DST_ADDR_INVALID;
124                 goto err_dst_addr;
125         }
126
127         memcpy(dst_addr, src_addr, reg->size);
128
129         pci_epc_unmap_addr(epc, dst_phys_addr);
130
131 err_dst_addr:
132         pci_epc_mem_free_addr(epc, dst_phys_addr, dst_addr, reg->size);
133
134 err_src_map_addr:
135         pci_epc_unmap_addr(epc, src_phys_addr);
136
137 err_src_addr:
138         pci_epc_mem_free_addr(epc, src_phys_addr, src_addr, reg->size);
139
140 err:
141         return ret;
142 }
143
144 static int pci_epf_test_read(struct pci_epf_test *epf_test)
145 {
146         int ret;
147         void __iomem *src_addr;
148         void *buf;
149         u32 crc32;
150         phys_addr_t phys_addr;
151         struct pci_epf *epf = epf_test->epf;
152         struct device *dev = &epf->dev;
153         struct pci_epc *epc = epf->epc;
154         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
155         struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
156
157         src_addr = pci_epc_mem_alloc_addr(epc, &phys_addr, reg->size);
158         if (!src_addr) {
159                 dev_err(dev, "failed to allocate address\n");
160                 reg->status = STATUS_SRC_ADDR_INVALID;
161                 ret = -ENOMEM;
162                 goto err;
163         }
164
165         ret = pci_epc_map_addr(epc, phys_addr, reg->src_addr, reg->size);
166         if (ret) {
167                 dev_err(dev, "failed to map address\n");
168                 reg->status = STATUS_SRC_ADDR_INVALID;
169                 goto err_addr;
170         }
171
172         buf = kzalloc(reg->size, GFP_KERNEL);
173         if (!buf) {
174                 ret = -ENOMEM;
175                 goto err_map_addr;
176         }
177
178         memcpy(buf, src_addr, reg->size);
179
180         crc32 = crc32_le(~0, buf, reg->size);
181         if (crc32 != reg->checksum)
182                 ret = -EIO;
183
184         kfree(buf);
185
186 err_map_addr:
187         pci_epc_unmap_addr(epc, phys_addr);
188
189 err_addr:
190         pci_epc_mem_free_addr(epc, phys_addr, src_addr, reg->size);
191
192 err:
193         return ret;
194 }
195
196 static int pci_epf_test_write(struct pci_epf_test *epf_test)
197 {
198         int ret;
199         void __iomem *dst_addr;
200         void *buf;
201         phys_addr_t phys_addr;
202         struct pci_epf *epf = epf_test->epf;
203         struct device *dev = &epf->dev;
204         struct pci_epc *epc = epf->epc;
205         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
206         struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
207
208         dst_addr = pci_epc_mem_alloc_addr(epc, &phys_addr, reg->size);
209         if (!dst_addr) {
210                 dev_err(dev, "failed to allocate address\n");
211                 reg->status = STATUS_DST_ADDR_INVALID;
212                 ret = -ENOMEM;
213                 goto err;
214         }
215
216         ret = pci_epc_map_addr(epc, phys_addr, reg->dst_addr, reg->size);
217         if (ret) {
218                 dev_err(dev, "failed to map address\n");
219                 reg->status = STATUS_DST_ADDR_INVALID;
220                 goto err_addr;
221         }
222
223         buf = kzalloc(reg->size, GFP_KERNEL);
224         if (!buf) {
225                 ret = -ENOMEM;
226                 goto err_map_addr;
227         }
228
229         get_random_bytes(buf, reg->size);
230         reg->checksum = crc32_le(~0, buf, reg->size);
231
232         memcpy(dst_addr, buf, reg->size);
233
234         /*
235          * wait 1ms inorder for the write to complete. Without this delay L3
236          * error in observed in the host system.
237          */
238         mdelay(1);
239
240         kfree(buf);
241
242 err_map_addr:
243         pci_epc_unmap_addr(epc, phys_addr);
244
245 err_addr:
246         pci_epc_mem_free_addr(epc, phys_addr, dst_addr, reg->size);
247
248 err:
249         return ret;
250 }
251
252 static void pci_epf_test_raise_irq(struct pci_epf_test *epf_test)
253 {
254         u8 irq;
255         u8 msi_count;
256         struct pci_epf *epf = epf_test->epf;
257         struct pci_epc *epc = epf->epc;
258         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
259         struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
260
261         reg->status |= STATUS_IRQ_RAISED;
262         msi_count = pci_epc_get_msi(epc);
263         irq = (reg->command & MSI_NUMBER_MASK) >> MSI_NUMBER_SHIFT;
264         if (irq > msi_count || msi_count <= 0)
265                 pci_epc_raise_irq(epc, PCI_EPC_IRQ_LEGACY, 0);
266         else
267                 pci_epc_raise_irq(epc, PCI_EPC_IRQ_MSI, irq);
268 }
269
270 static void pci_epf_test_cmd_handler(struct work_struct *work)
271 {
272         int ret;
273         u8 irq;
274         u8 msi_count;
275         u32 command;
276         struct pci_epf_test *epf_test = container_of(work, struct pci_epf_test,
277                                                      cmd_handler.work);
278         struct pci_epf *epf = epf_test->epf;
279         struct pci_epc *epc = epf->epc;
280         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
281         struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
282
283         command = reg->command;
284         if (!command)
285                 goto reset_handler;
286
287         reg->command = 0;
288         reg->status = 0;
289
290         if (command & COMMAND_RAISE_LEGACY_IRQ) {
291                 reg->status = STATUS_IRQ_RAISED;
292                 pci_epc_raise_irq(epc, PCI_EPC_IRQ_LEGACY, 0);
293                 goto reset_handler;
294         }
295
296         if (command & COMMAND_WRITE) {
297                 ret = pci_epf_test_write(epf_test);
298                 if (ret)
299                         reg->status |= STATUS_WRITE_FAIL;
300                 else
301                         reg->status |= STATUS_WRITE_SUCCESS;
302                 pci_epf_test_raise_irq(epf_test);
303                 goto reset_handler;
304         }
305
306         if (command & COMMAND_READ) {
307                 ret = pci_epf_test_read(epf_test);
308                 if (!ret)
309                         reg->status |= STATUS_READ_SUCCESS;
310                 else
311                         reg->status |= STATUS_READ_FAIL;
312                 pci_epf_test_raise_irq(epf_test);
313                 goto reset_handler;
314         }
315
316         if (command & COMMAND_COPY) {
317                 ret = pci_epf_test_copy(epf_test);
318                 if (!ret)
319                         reg->status |= STATUS_COPY_SUCCESS;
320                 else
321                         reg->status |= STATUS_COPY_FAIL;
322                 pci_epf_test_raise_irq(epf_test);
323                 goto reset_handler;
324         }
325
326         if (command & COMMAND_RAISE_MSI_IRQ) {
327                 msi_count = pci_epc_get_msi(epc);
328                 irq = (command & MSI_NUMBER_MASK) >> MSI_NUMBER_SHIFT;
329                 if (irq > msi_count || msi_count <= 0)
330                         goto reset_handler;
331                 reg->status = STATUS_IRQ_RAISED;
332                 pci_epc_raise_irq(epc, PCI_EPC_IRQ_MSI, irq);
333                 goto reset_handler;
334         }
335
336 reset_handler:
337         queue_delayed_work(kpcitest_workqueue, &epf_test->cmd_handler,
338                            msecs_to_jiffies(1));
339 }
340
341 static void pci_epf_test_linkup(struct pci_epf *epf)
342 {
343         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
344
345         queue_delayed_work(kpcitest_workqueue, &epf_test->cmd_handler,
346                            msecs_to_jiffies(1));
347 }
348
349 static void pci_epf_test_unbind(struct pci_epf *epf)
350 {
351         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
352         struct pci_epc *epc = epf->epc;
353         int bar;
354
355         cancel_delayed_work(&epf_test->cmd_handler);
356         pci_epc_stop(epc);
357         for (bar = BAR_0; bar <= BAR_5; bar++) {
358                 if (epf_test->reg[bar]) {
359                         pci_epf_free_space(epf, epf_test->reg[bar], bar);
360                         pci_epc_clear_bar(epc, bar);
361                 }
362         }
363 }
364
365 static int pci_epf_test_set_bar(struct pci_epf *epf)
366 {
367         int flags;
368         int bar;
369         int ret;
370         struct pci_epf_bar *epf_bar;
371         struct pci_epc *epc = epf->epc;
372         struct device *dev = &epf->dev;
373         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
374         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
375
376         flags = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32;
377         if (sizeof(dma_addr_t) == 0x8)
378                 flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
379
380         for (bar = BAR_0; bar <= BAR_5; bar++) {
381                 epf_bar = &epf->bar[bar];
382                 ret = pci_epc_set_bar(epc, bar, epf_bar->phys_addr,
383                                       epf_bar->size, flags);
384                 if (ret) {
385                         pci_epf_free_space(epf, epf_test->reg[bar], bar);
386                         dev_err(dev, "failed to set BAR%d\n", bar);
387                         if (bar == test_reg_bar)
388                                 return ret;
389                 }
390         }
391
392         return 0;
393 }
394
395 static int pci_epf_test_alloc_space(struct pci_epf *epf)
396 {
397         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
398         struct device *dev = &epf->dev;
399         void *base;
400         int bar;
401         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
402
403         base = pci_epf_alloc_space(epf, sizeof(struct pci_epf_test_reg),
404                                    test_reg_bar);
405         if (!base) {
406                 dev_err(dev, "failed to allocated register space\n");
407                 return -ENOMEM;
408         }
409         epf_test->reg[test_reg_bar] = base;
410
411         for (bar = BAR_0; bar <= BAR_5; bar++) {
412                 if (bar == test_reg_bar)
413                         continue;
414                 base = pci_epf_alloc_space(epf, bar_size[bar], bar);
415                 if (!base)
416                         dev_err(dev, "failed to allocate space for BAR%d\n",
417                                 bar);
418                 epf_test->reg[bar] = base;
419         }
420
421         return 0;
422 }
423
424 static int pci_epf_test_bind(struct pci_epf *epf)
425 {
426         int ret;
427         struct pci_epf_header *header = epf->header;
428         struct pci_epc *epc = epf->epc;
429         struct device *dev = &epf->dev;
430
431         if (WARN_ON_ONCE(!epc))
432                 return -EINVAL;
433
434         ret = pci_epc_write_header(epc, header);
435         if (ret) {
436                 dev_err(dev, "configuration header write failed\n");
437                 return ret;
438         }
439
440         ret = pci_epf_test_alloc_space(epf);
441         if (ret)
442                 return ret;
443
444         ret = pci_epf_test_set_bar(epf);
445         if (ret)
446                 return ret;
447
448         ret = pci_epc_set_msi(epc, epf->msi_interrupts);
449         if (ret)
450                 return ret;
451
452         return 0;
453 }
454
455 static const struct pci_epf_device_id pci_epf_test_ids[] = {
456         {
457                 .name = "pci_epf_test",
458         },
459         {},
460 };
461
462 static int pci_epf_test_probe(struct pci_epf *epf)
463 {
464         struct pci_epf_test *epf_test;
465         struct device *dev = &epf->dev;
466         const struct pci_epf_device_id *match;
467         struct pci_epf_test_data *data;
468         enum pci_barno test_reg_bar = BAR_0;
469
470         match = pci_epf_match_device(pci_epf_test_ids, epf);
471         data = (struct pci_epf_test_data *)match->driver_data;
472         if (data)
473                 test_reg_bar = data->test_reg_bar;
474
475         epf_test = devm_kzalloc(dev, sizeof(*epf_test), GFP_KERNEL);
476         if (!epf_test)
477                 return -ENOMEM;
478
479         epf->header = &test_header;
480         epf_test->epf = epf;
481         epf_test->test_reg_bar = test_reg_bar;
482
483         INIT_DELAYED_WORK(&epf_test->cmd_handler, pci_epf_test_cmd_handler);
484
485         epf_set_drvdata(epf, epf_test);
486         return 0;
487 }
488
489 static int pci_epf_test_remove(struct pci_epf *epf)
490 {
491         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
492
493         kfree(epf_test);
494         return 0;
495 }
496
497 static struct pci_epf_ops ops = {
498         .unbind = pci_epf_test_unbind,
499         .bind   = pci_epf_test_bind,
500         .linkup = pci_epf_test_linkup,
501 };
502
503 static struct pci_epf_driver test_driver = {
504         .driver.name    = "pci_epf_test",
505         .probe          = pci_epf_test_probe,
506         .remove         = pci_epf_test_remove,
507         .id_table       = pci_epf_test_ids,
508         .ops            = &ops,
509         .owner          = THIS_MODULE,
510 };
511
512 static int __init pci_epf_test_init(void)
513 {
514         int ret;
515
516         kpcitest_workqueue = alloc_workqueue("kpcitest",
517                                              WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
518         ret = pci_epf_register_driver(&test_driver);
519         if (ret) {
520                 pr_err("failed to register pci epf test driver --> %d\n", ret);
521                 return ret;
522         }
523
524         return 0;
525 }
526 module_init(pci_epf_test_init);
527
528 static void __exit pci_epf_test_exit(void)
529 {
530         pci_epf_unregister_driver(&test_driver);
531 }
532 module_exit(pci_epf_test_exit);
533
534 MODULE_DESCRIPTION("PCI EPF TEST DRIVER");
535 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
536 MODULE_LICENSE("GPL v2");