1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * This pci and pci-express testing kernel module will allow test calls
20 * to be driven through various ioctl calls in a
21 * user space program that has attained the appropriate
22 * file descriptor for this device. For the functions of
23 * this module to work correctly there must be a pci / pci-express
24 * device somewhere in the system. The tests do not need
25 * a specific device, and the first pci device available
26 * will be grabbed.
27 *
28 * author: Sean Ruyle (srruyle@us.ibm.com)
29 * date: 5/20/2003
30 * PCI-Express test scripts author: Amit Khanna (amit.khanna@intel.com)
31 * date: 8/20/2004
32 *
33 * file: tpci.c,
34 * module: tpci
35 */
36
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <linux/fs.h>
40 #include <linux/ioctl.h>
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/pci.h>
44
45 #include "tpci.h"
46
47 MODULE_AUTHOR("Sean Ruyle <srruyle@us.ibm.com>");
48 MODULE_AUTHOR("Amit Khanna <amit.khanna@intel.com>");
49 MODULE_AUTHOR("Copyright (c) 2013 Oracle and/or its affiliates");
50 MODULE_DESCRIPTION("LTP PCI Test");
51 MODULE_LICENSE("GPL");
52
53 #define prk_err(fmt, ...) \
54 pr_err(PCI_DEVICE_NAME ": " fmt "\n", ##__VA_ARGS__)
55 #define prk_info(fmt, ...) \
56 pr_info(PCI_DEVICE_NAME ": " fmt "\n", ##__VA_ARGS__)
57 #define prk_debug(fmt, ...) \
58 pr_debug(PCI_DEVICE_NAME ": " fmt "\n", ##__VA_ARGS__)
59
60 #define TPASS 0
61 #define TFAIL 1
62 #define TSKIP 32
63
64 static const struct pci_device_id ltp_pci_tbl[] = {
65 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
66 { 0, }
67 };
68
69 MODULE_DEVICE_TABLE(pci, ltp_pci_tbl);
70
ltp_pci_probe(struct pci_dev * pci_dev,const struct pci_device_id * pci_ent)71 static int ltp_pci_probe(struct pci_dev *pci_dev,
72 const struct pci_device_id *pci_ent)
73 {
74 return 0;
75 }
76
77 static struct pci_driver ltp_pci_driver = {
78 .name = "LTP_PCI_DRIVER",
79 .id_table = ltp_pci_tbl,
80 .probe = ltp_pci_probe,
81 };
82
83 static int pci_registered;
84
85 struct tpci_user {
86 struct pci_dev *dev;
87 struct pci_bus *bus;
88 struct pci_driver *drv;
89 uint32_t state[16];
90 };
91 static struct tpci_user ltp_pci;
92
93 /*
94 * probe_pci_dev
95 * find a pci device that can be used for other test
96 * calls in this kernel module.
97 */
probe_pci_dev(unsigned int bus,unsigned int slot)98 static int probe_pci_dev(unsigned int bus, unsigned int slot)
99 {
100 struct pci_dev *dev;
101
102 if (ltp_pci.dev) {
103 pci_dev_put(ltp_pci.dev);
104 ltp_pci.dev = NULL;
105 }
106
107 dev = pci_get_domain_bus_and_slot(0, bus, slot);
108 if (!dev || !dev->driver)
109 return -ENODEV;
110
111 prk_info("found pci_dev '%s', bus %u, devfn %u",
112 pci_name(dev), bus, slot);
113
114 ltp_pci.dev = dev;
115 ltp_pci.bus = dev->bus;
116 prk_info("Bus number: %d", dev->bus->number);
117 return 0;
118 }
119
120 /*
121 * pci_enable
122 * enable a pci device so that it may be used in
123 * later testing in the user test program
124 */
pci_enable(void)125 static int pci_enable(void)
126 {
127 struct pci_dev *dev = ltp_pci.dev;
128
129 prk_info("enable pci device");
130
131 /* check if can enable the device pointer */
132 if (!dev) {
133 prk_err("dev is NULL");
134 return TFAIL;
135 }
136
137 if (pci_enable_device(dev)) {
138 prk_err("failed to enable pci device");
139 return TFAIL;
140 }
141
142 prk_info("enabled pci device");
143 return TPASS;
144 }
145
pci_disable(void)146 static int pci_disable(void)
147 {
148 struct pci_dev *dev = ltp_pci.dev;
149
150 prk_info("disable pci device");
151
152 /* check if device pointer exists */
153 if (!dev) {
154 prk_err("dev is NULL");
155 return TFAIL;
156 }
157
158 prk_info("is pci enabled '%d', is managed '%d'",
159 pci_is_enabled(dev), pci_is_managed(dev));
160
161 pci_release_regions(dev);
162 pci_disable_device(dev);
163
164 if (dev->current_state == PCI_D3hot ||
165 dev->current_state == PCI_D3cold) {
166
167 prk_info("disabled pci device, state '%s'",
168 pci_power_name(dev->current_state));
169 return TPASS;
170
171 }
172
173 prk_err("failed to disable pci device, state '%s'",
174 pci_power_name(dev->current_state));
175 return TFAIL;
176 }
177
178 /*
179 * find_bus
180 * call to pci_find_bus, use values from bus
181 * pointer in ltp_pci, make sure that returns
182 * bus with same values
183 */
test_find_bus(void)184 static int test_find_bus(void)
185 {
186 int num = ltp_pci.bus->number;
187 struct pci_bus *temp = NULL;
188
189 prk_info("find bus");
190
191 temp = pci_find_bus(pci_domain_nr(ltp_pci.bus), num);
192
193 if (!temp) {
194 prk_info("pci_find_bus failed");
195 return TFAIL;
196 } else if (temp->number != num) {
197 prk_err("returned bus pointer w/ wrong bus number");
198 return TFAIL;
199 }
200
201 prk_info("success returned bus pointer");
202 return TPASS;
203 }
204
205 /*
206 * find_class
207 * call to pci_find_class, using values from the
208 * pci_dev pointer in ltp_pci structure
209 */
test_find_class(void)210 static int test_find_class(void)
211 {
212 unsigned int num = ltp_pci.dev->class;
213 struct pci_dev *temp = NULL;
214
215 prk_info("find pci class");
216
217 temp = pci_get_class(num, NULL);
218
219 if (!temp) {
220 prk_err("failed to find pci device from class number");
221 return TFAIL;
222 }
223
224 prk_info("found pci device from class number");
225 pci_dev_put(temp);
226
227 return TPASS;
228 }
229
230 /*
231 * find_device
232 * call to pci_find_device, using values for
233 * parameters from pci_dev pointer in the
234 * ltp_pci structure
235 */
test_find_device(void)236 static int test_find_device(void)
237 {
238 struct pci_dev *temp = NULL;
239 unsigned short ven = ltp_pci.dev->vendor, dev = ltp_pci.dev->device;
240
241 prk_info("get pci device");
242
243 temp = pci_get_device(ven, dev, NULL);
244
245 if (!temp) {
246 prk_err("failed to find pci device from device info");
247 return TFAIL;
248 }
249
250 prk_info("found pci device from device info");
251 pci_dev_put(temp);
252
253 return TPASS;
254 }
255
256 /*
257 * find_subsys
258 * call to pci_find_subsys, use valued from
259 * pci_dev pointer in ltp_pci structure to
260 * find pci_dev from subsys info
261 */
test_find_subsys(void)262 static int test_find_subsys(void)
263 {
264 struct pci_dev *temp;
265 unsigned short ven = ltp_pci.dev->vendor,
266 dev = ltp_pci.dev->device,
267 ss_ven = ltp_pci.dev->subsystem_vendor,
268 ss_dev = ltp_pci.dev->subsystem_device;
269
270 prk_info("get pci subsys");
271 temp = pci_get_subsys(ven, dev, ss_ven, ss_dev, NULL);
272
273 if (!temp) {
274 prk_err("failed to find pci device from subsys info");
275 return TFAIL;
276 }
277
278 prk_info("found pci device from subsys info");
279 pci_dev_put(temp);
280
281 return TPASS;
282 }
283
284 /*
285 * test_scan_bus
286 * call to pci_do_scan_bus, which takes
287 * a struct pci_bus pointer, which will
288 * return an integer for how far the
289 * function got in scanning bus
290 */
test_scan_bus(void)291 static int test_scan_bus(void)
292 {
293 #ifdef CONFIG_HOTPLUG
294 int num;
295 struct pci_bus *bus = ltp_pci.bus;
296
297 prk_info("scan pci bus");
298
299 num = pci_rescan_bus(bus);
300 /*
301 * check if returned number is greater than
302 * max number of bus or less than 0
303 */
304 if (num > MAX_BUS || num < 0) {
305 prk_err("failed scan bus");
306 return TFAIL;
307 }
308 prk_info("success scan bus");
309 return TPASS;
310 #else
311 prk_info("pci_rescan_bus() is not supported");
312 return TSKIP;
313 #endif
314 }
315
316 /*
317 * test_slot_scan
318 * make call to pci_scan_slot, which will
319 * find the device pointer and setup the
320 * device info
321 */
test_slot_scan(void)322 static int test_slot_scan(void)
323 {
324 int ret, num = ltp_pci.dev->devfn;
325 struct pci_bus *bus = ltp_pci.bus;
326
327 prk_info("scan pci slot");
328
329 ret = pci_scan_slot(bus, num);
330 if (ret >= 0) {
331 prk_info("found '%d' devices from scan slot", ret);
332 return TPASS;
333 }
334
335 prk_err("pci_scan_slot failed");
336 return TFAIL;
337 }
338
339 /*
340 * test_bus_add_devices
341 * make call to pci_bus_add_devices,
342 * which will check the device pointer
343 * that is passed in for more devices
344 * that it can add
345 */
test_bus_add_devices(void)346 static int test_bus_add_devices(void)
347 {
348 struct pci_bus *bus = ltp_pci.bus;
349
350 prk_info("add bus device");
351
352 pci_bus_add_devices(bus);
353
354 if (bus) {
355 prk_info("called bus_add_device");
356 return TPASS;
357 }
358
359 prk_err("bus_add_device failed");
360 return TFAIL;
361 }
362
363 /*
364 * test_match_device
365 * make call to pci_match_device, returns a
366 * pci_device_id pointer
367 */
test_match_device(void)368 static int test_match_device(void)
369 {
370 struct pci_dev *dev = ltp_pci.dev;
371 struct pci_driver *drv;
372 const struct pci_device_id *id;
373
374 prk_info("test pci_device_id()");
375
376 drv = pci_dev_driver(dev);
377
378 if (!drv) {
379 prk_err("driver pointer not allocated for pci_dev");
380 return TFAIL;
381 }
382
383 id = pci_match_id(drv->id_table, dev);
384
385 if (id) {
386 prk_info("match device success");
387 return TPASS;
388 }
389
390 prk_err("failed return pci_device_id");
391 return TFAIL;
392 }
393
394
395 /*
396 * test_reg_driver
397 * make call to pci_register_driver, which will
398 * register the driver for a device with the
399 * system
400 */
test_reg_driver(void)401 static int test_reg_driver(void)
402 {
403 prk_info("test pci_register_driver");
404 if (pci_register_driver(<p_pci_driver)) {
405 prk_err("unsuccessful registering pci driver");
406 return TFAIL;
407 }
408 pci_registered = 1;
409 prk_info("success driver register");
410 return TPASS;
411 }
412
413 /*
414 * test_unreg_driver
415 * make call to pci_unregister_driver, which will
416 * unregister the driver for a device from the system
417 */
test_unreg_driver(void)418 static int test_unreg_driver(void)
419 {
420 pci_unregister_driver(<p_pci_driver);
421 pci_registered = 0;
422 return TPASS;
423 }
424
425 /*
426 * test_assign_resources
427 * make calls to pci_assign_resource, will need
428 * to setup a dev pointer and resource pointer,
429 */
test_assign_resources(void)430 static int test_assign_resources(void)
431 {
432 int i, ret, rc = 0;
433 struct pci_dev *dev = ltp_pci.dev;
434 struct resource *r;
435
436 prk_info("assign resources");
437
438 for (i = 0; i < 7; ++i) {
439 prk_info("assign resource #%d", i);
440 r = &dev->resource[i];
441 prk_info("name = %s, flags = %lu, start 0x%lx, end 0x%lx",
442 r->name, r->flags,
443 (unsigned long)r->start, (unsigned long)r->end);
444
445 if (r->flags & IORESOURCE_MEM &&
446 r->flags & IORESOURCE_PREFETCH) {
447 ret = pci_assign_resource(dev, i);
448 prk_info("assign resource to '%d', ret '%d'", i, ret);
449 rc |= (ret < 0 && ret != -EBUSY) ? TFAIL : TPASS;
450 }
451 }
452
453 /*
454 * enable device after call to assign resource
455 * because might error if (!r->start && r->end)
456 */
457 if (pci_enable_device(dev))
458 return TFAIL;
459
460 return rc;
461 }
462
463 /*
464 * test_save_state
465 * make call to pci_save_state, takes in a u32*
466 * buffer
467 */
test_save_state(void)468 static int test_save_state(void)
469 {
470 struct pci_dev *dev = ltp_pci.dev;
471
472 prk_info("save state");
473
474 if (pci_save_state(dev)) {
475 prk_err("failed save state");
476 return TFAIL;
477 }
478
479 prk_info("saved state of device");
480 return TPASS;
481 }
482
483 /*
484 * test_restore_state
485 * make call to pci_restore_state, get the state buffer
486 * should have been previously filled out by save state
487 */
test_restore_state(void)488 static int test_restore_state(void)
489 {
490 struct pci_dev *dev = ltp_pci.dev;
491
492 prk_info("restore state");
493
494 pci_restore_state(dev);
495
496 return TPASS;
497 }
498
499 /*
500 * test_find_cap
501 * make call to pci_find_capability, which
502 * will determine if a device has a certain
503 * capability, use second parameter to specify
504 * which capability you are looking for
505 */
test_find_cap(void)506 static int test_find_cap(void)
507 {
508 struct pci_dev *dev = ltp_pci.dev;
509
510 prk_info("find device capability");
511
512 if (pci_find_capability(dev, PCI_CAP_ID_PM))
513 prk_info("does not have tested capability");
514 else
515 prk_info("device has PM capability");
516
517 return TPASS;
518 }
519
520 /*
521 * test_read_pci_exp_config
522 * make call to pci_config_read and determine if
523 * the PCI-Express enhanced config space of this
524 * device can be read successfully.
525 */
test_read_pci_exp_config(void)526 static int test_read_pci_exp_config(void)
527 {
528 int pos;
529 u32 header;
530 struct pci_dev *dev = ltp_pci.dev;
531
532 /* skip the test if device doesn't have PCIe capability */
533 pos = pci_pcie_cap(dev);
534 if (!pos) {
535 prk_info("device doesn't have PCI-EXP capability");
536 return TSKIP;
537 }
538 prk_info("read the PCI Express configuration registers at 0x%x", pos);
539
540 if (pci_read_config_dword(dev, pos, &header)) {
541 prk_err("failed to read config dword");
542 return TFAIL;
543 }
544
545 /* comparing the value read with PCI_CAP_ID_EXP macro */
546 if ((header & 0x000000ff) == PCI_CAP_ID_EXP) {
547 prk_info("correct val read using PCIE driver installed: 0x%x",
548 header);
549 return TPASS;
550 }
551
552 prk_err("incorrect val read. PCIE driver/device not installed: 0x%x",
553 header);
554 return TFAIL;
555 }
556
test_case(unsigned int cmd)557 static int test_case(unsigned int cmd)
558 {
559 int rc = TSKIP;
560
561 switch (cmd) {
562 case PCI_ENABLE:
563 rc = pci_enable();
564 break;
565 case PCI_DISABLE:
566 rc = pci_disable();
567 break;
568 case FIND_BUS:
569 rc = test_find_bus();
570 break;
571 case FIND_CLASS:
572 rc = test_find_class();
573 break;
574 case FIND_DEVICE:
575 rc = test_find_device();
576 break;
577 case FIND_SUBSYS:
578 rc = test_find_subsys();
579 break;
580 case BUS_SCAN:
581 rc = test_scan_bus();
582 break;
583 case SLOT_SCAN:
584 rc = test_slot_scan();
585 break;
586 case BUS_ADD_DEVICES:
587 rc = test_bus_add_devices();
588 break;
589 case MATCH_DEVICE:
590 rc = test_match_device();
591 break;
592 case REG_DRIVER:
593 rc = test_reg_driver();
594 break;
595 case UNREG_DRIVER:
596 rc = test_unreg_driver();
597 break;
598 case PCI_RESOURCES:
599 rc = test_assign_resources();
600 break;
601 case SAVE_STATE:
602 rc = test_save_state();
603 break;
604 case RESTORE_STATE:
605 rc = test_restore_state();
606 break;
607 case FIND_CAP:
608 rc = test_find_cap();
609 break;
610 case PCI_EXP_CAP_CONFIG:
611 rc = test_read_pci_exp_config();
612 break;
613 default:
614 prk_info("mismatching test-case command %d", cmd);
615 break;
616 }
617
618 return rc;
619 }
620
621 /*
622 * Test-case result,
623 * if test is passed, value will be set to 0
624 */
625 static int test_result;
626
device_release(struct device * dev)627 static void device_release(struct device *dev)
628 {
629 prk_info("device released\n");
630 }
631
632 static struct device tdev = {
633 .init_name = PCI_DEVICE_NAME,
634 .release = device_release,
635 };
636
637 /* print test result to sysfs file */
sys_result(struct device * dev,struct device_attribute * attr,char * buf)638 static ssize_t sys_result(struct device *dev,
639 struct device_attribute *attr, char *buf)
640 {
641 return scnprintf(buf, PAGE_SIZE, "%d\n", test_result);
642 }
643 static DEVICE_ATTR(result, S_IRUSR, sys_result, NULL);
644
sys_tcase(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)645 static ssize_t sys_tcase(struct device *dev,
646 struct device_attribute *attr, const char *buf, size_t count)
647 {
648 int tc = 0;
649
650 sscanf(buf, "%d", &tc);
651 prk_info("test-case %d", tc);
652
653 test_result = test_case(tc);
654
655 return count;
656 }
657 static DEVICE_ATTR(tcase, S_IWUSR, NULL, sys_tcase);
658
sys_bus_slot(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)659 static ssize_t sys_bus_slot(struct device *dev,
660 struct device_attribute *attr, const char *buf, size_t count)
661 {
662 unsigned int res, bus, slot;
663 int ret;
664
665 sscanf(buf, "%u", &res);
666
667 bus = res >> 8 & 0xFF;
668 slot = res & 0xFF;
669
670 ret = probe_pci_dev(bus, slot);
671 if (ret)
672 return ret;
673
674 return count;
675 }
676 static DEVICE_ATTR(bus_slot, S_IWUSR, NULL, sys_bus_slot);
677
tpci_init_module(void)678 static int tpci_init_module(void)
679 {
680 int err = 0;
681 prk_info("Starting module");
682
683 err = device_register(&tdev);
684 if (err) {
685 prk_err("Unable to register device");
686 goto err0;
687 }
688 prk_info("device registered\n");
689
690 err = device_create_file(&tdev, &dev_attr_result);
691 if (err) {
692 prk_err("Can't create sysfs file 'result'");
693 goto err1;
694 }
695
696 err = device_create_file(&tdev, &dev_attr_tcase);
697 if (err) {
698 prk_err(": Can't create sysfs file 'tc'");
699 goto err2;
700 }
701
702 err = device_create_file(&tdev, &dev_attr_bus_slot);
703 if (err) {
704 prk_err(": Can't create sysfs file 'bus_slot'");
705 goto err3;
706 }
707
708 return 0;
709
710 err3:
711 device_remove_file(&tdev, &dev_attr_tcase);
712 err2:
713 device_remove_file(&tdev, &dev_attr_result);
714 err1:
715 device_unregister(&tdev);
716 err0:
717 return err;
718 }
module_init(tpci_init_module)719 module_init(tpci_init_module)
720
721 static void tpci_exit_module(void)
722 {
723 prk_debug("Unloading module\n");
724 if (ltp_pci.dev)
725 pci_dev_put(ltp_pci.dev);
726
727 if (pci_registered)
728 pci_unregister_driver(<p_pci_driver);
729
730 device_remove_file(&tdev, &dev_attr_result);
731 device_remove_file(&tdev, &dev_attr_tcase);
732 device_remove_file(&tdev, &dev_attr_bus_slot);
733 device_unregister(&tdev);
734 }
735 module_exit(tpci_exit_module)
736