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