1 /* 2 * RapidIO interconnect services 3 * (RapidIO Interconnect Specification, http://www.rapidio.org) 4 * 5 * Copyright 2005 MontaVista Software, Inc. 6 * Matt Porter <mporter@kernel.crashing.org> 7 * 8 * Copyright 2009 - 2013 Integrated Device Technology, Inc. 9 * Alex Bounine <alexandre.bounine@idt.com> 10 * 11 * This program is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License as published by the 13 * Free Software Foundation; either version 2 of the License, or (at your 14 * option) any later version. 15 */ 16 17 #include <linux/types.h> 18 #include <linux/kernel.h> 19 20 #include <linux/delay.h> 21 #include <linux/init.h> 22 #include <linux/rio.h> 23 #include <linux/rio_drv.h> 24 #include <linux/rio_ids.h> 25 #include <linux/rio_regs.h> 26 #include <linux/module.h> 27 #include <linux/spinlock.h> 28 #include <linux/slab.h> 29 #include <linux/interrupt.h> 30 31 #include "rio.h" 32 33 /* 34 * struct rio_pwrite - RIO portwrite event 35 * @node: Node in list of doorbell events 36 * @pwcback: Doorbell event callback 37 * @context: Handler specific context to pass on event 38 */ 39 struct rio_pwrite { 40 struct list_head node; 41 42 int (*pwcback)(struct rio_mport *mport, void *context, 43 union rio_pw_msg *msg, int step); 44 void *context; 45 }; 46 47 MODULE_DESCRIPTION("RapidIO Subsystem Core"); 48 MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>"); 49 MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>"); 50 MODULE_LICENSE("GPL"); 51 52 static int hdid[RIO_MAX_MPORTS]; 53 static int ids_num; 54 module_param_array(hdid, int, &ids_num, 0); 55 MODULE_PARM_DESC(hdid, 56 "Destination ID assignment to local RapidIO controllers"); 57 58 static LIST_HEAD(rio_devices); 59 static LIST_HEAD(rio_nets); 60 static DEFINE_SPINLOCK(rio_global_list_lock); 61 62 static LIST_HEAD(rio_mports); 63 static LIST_HEAD(rio_scans); 64 static DEFINE_MUTEX(rio_mport_list_lock); 65 static unsigned char next_portid; 66 static DEFINE_SPINLOCK(rio_mmap_lock); 67 68 /** 69 * rio_local_get_device_id - Get the base/extended device id for a port 70 * @port: RIO master port from which to get the deviceid 71 * 72 * Reads the base/extended device id from the local device 73 * implementing the master port. Returns the 8/16-bit device 74 * id. 75 */ 76 u16 rio_local_get_device_id(struct rio_mport *port) 77 { 78 u32 result; 79 80 rio_local_read_config_32(port, RIO_DID_CSR, &result); 81 82 return (RIO_GET_DID(port->sys_size, result)); 83 } 84 85 /** 86 * rio_query_mport - Query mport device attributes 87 * @port: mport device to query 88 * @mport_attr: mport attributes data structure 89 * 90 * Returns attributes of specified mport through the 91 * pointer to attributes data structure. 92 */ 93 int rio_query_mport(struct rio_mport *port, 94 struct rio_mport_attr *mport_attr) 95 { 96 if (!port->ops->query_mport) 97 return -ENODATA; 98 return port->ops->query_mport(port, mport_attr); 99 } 100 EXPORT_SYMBOL(rio_query_mport); 101 102 /** 103 * rio_alloc_net- Allocate and initialize a new RIO network data structure 104 * @mport: Master port associated with the RIO network 105 * 106 * Allocates a RIO network structure, initializes per-network 107 * list heads, and adds the associated master port to the 108 * network list of associated master ports. Returns a 109 * RIO network pointer on success or %NULL on failure. 110 */ 111 struct rio_net *rio_alloc_net(struct rio_mport *mport) 112 { 113 struct rio_net *net; 114 115 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL); 116 if (net) { 117 INIT_LIST_HEAD(&net->node); 118 INIT_LIST_HEAD(&net->devices); 119 INIT_LIST_HEAD(&net->switches); 120 INIT_LIST_HEAD(&net->mports); 121 mport->net = net; 122 } 123 return net; 124 } 125 EXPORT_SYMBOL_GPL(rio_alloc_net); 126 127 int rio_add_net(struct rio_net *net) 128 { 129 int err; 130 131 err = device_register(&net->dev); 132 if (err) 133 return err; 134 spin_lock(&rio_global_list_lock); 135 list_add_tail(&net->node, &rio_nets); 136 spin_unlock(&rio_global_list_lock); 137 138 return 0; 139 } 140 EXPORT_SYMBOL_GPL(rio_add_net); 141 142 void rio_free_net(struct rio_net *net) 143 { 144 spin_lock(&rio_global_list_lock); 145 if (!list_empty(&net->node)) 146 list_del(&net->node); 147 spin_unlock(&rio_global_list_lock); 148 if (net->release) 149 net->release(net); 150 device_unregister(&net->dev); 151 } 152 EXPORT_SYMBOL_GPL(rio_free_net); 153 154 /** 155 * rio_local_set_device_id - Set the base/extended device id for a port 156 * @port: RIO master port 157 * @did: Device ID value to be written 158 * 159 * Writes the base/extended device id from a device. 160 */ 161 void rio_local_set_device_id(struct rio_mport *port, u16 did) 162 { 163 rio_local_write_config_32(port, RIO_DID_CSR, 164 RIO_SET_DID(port->sys_size, did)); 165 } 166 EXPORT_SYMBOL_GPL(rio_local_set_device_id); 167 168 /** 169 * rio_add_device- Adds a RIO device to the device model 170 * @rdev: RIO device 171 * 172 * Adds the RIO device to the global device list and adds the RIO 173 * device to the RIO device list. Creates the generic sysfs nodes 174 * for an RIO device. 175 */ 176 int rio_add_device(struct rio_dev *rdev) 177 { 178 int err; 179 180 atomic_set(&rdev->state, RIO_DEVICE_RUNNING); 181 err = device_register(&rdev->dev); 182 if (err) 183 return err; 184 185 spin_lock(&rio_global_list_lock); 186 list_add_tail(&rdev->global_list, &rio_devices); 187 if (rdev->net) { 188 list_add_tail(&rdev->net_list, &rdev->net->devices); 189 if (rdev->pef & RIO_PEF_SWITCH) 190 list_add_tail(&rdev->rswitch->node, 191 &rdev->net->switches); 192 } 193 spin_unlock(&rio_global_list_lock); 194 195 rio_create_sysfs_dev_files(rdev); 196 197 return 0; 198 } 199 EXPORT_SYMBOL_GPL(rio_add_device); 200 201 /* 202 * rio_del_device - removes a RIO device from the device model 203 * @rdev: RIO device 204 * @state: device state to set during removal process 205 * 206 * Removes the RIO device to the kernel device list and subsystem's device list. 207 * Clears sysfs entries for the removed device. 208 */ 209 void rio_del_device(struct rio_dev *rdev, enum rio_device_state state) 210 { 211 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev)); 212 atomic_set(&rdev->state, state); 213 spin_lock(&rio_global_list_lock); 214 list_del(&rdev->global_list); 215 if (rdev->net) { 216 list_del(&rdev->net_list); 217 if (rdev->pef & RIO_PEF_SWITCH) { 218 list_del(&rdev->rswitch->node); 219 kfree(rdev->rswitch->route_table); 220 } 221 } 222 spin_unlock(&rio_global_list_lock); 223 rio_remove_sysfs_dev_files(rdev); 224 device_unregister(&rdev->dev); 225 } 226 EXPORT_SYMBOL_GPL(rio_del_device); 227 228 /** 229 * rio_request_inb_mbox - request inbound mailbox service 230 * @mport: RIO master port from which to allocate the mailbox resource 231 * @dev_id: Device specific pointer to pass on event 232 * @mbox: Mailbox number to claim 233 * @entries: Number of entries in inbound mailbox queue 234 * @minb: Callback to execute when inbound message is received 235 * 236 * Requests ownership of an inbound mailbox resource and binds 237 * a callback function to the resource. Returns %0 on success. 238 */ 239 int rio_request_inb_mbox(struct rio_mport *mport, 240 void *dev_id, 241 int mbox, 242 int entries, 243 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox, 244 int slot)) 245 { 246 int rc = -ENOSYS; 247 struct resource *res; 248 249 if (mport->ops->open_inb_mbox == NULL) 250 goto out; 251 252 res = kzalloc(sizeof(struct resource), GFP_KERNEL); 253 254 if (res) { 255 rio_init_mbox_res(res, mbox, mbox); 256 257 /* Make sure this mailbox isn't in use */ 258 if ((rc = 259 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE], 260 res)) < 0) { 261 kfree(res); 262 goto out; 263 } 264 265 mport->inb_msg[mbox].res = res; 266 267 /* Hook the inbound message callback */ 268 mport->inb_msg[mbox].mcback = minb; 269 270 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries); 271 if (rc) { 272 mport->inb_msg[mbox].mcback = NULL; 273 mport->inb_msg[mbox].res = NULL; 274 release_resource(res); 275 kfree(res); 276 } 277 } else 278 rc = -ENOMEM; 279 280 out: 281 return rc; 282 } 283 284 /** 285 * rio_release_inb_mbox - release inbound mailbox message service 286 * @mport: RIO master port from which to release the mailbox resource 287 * @mbox: Mailbox number to release 288 * 289 * Releases ownership of an inbound mailbox resource. Returns 0 290 * if the request has been satisfied. 291 */ 292 int rio_release_inb_mbox(struct rio_mport *mport, int mbox) 293 { 294 int rc; 295 296 if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res) 297 return -EINVAL; 298 299 mport->ops->close_inb_mbox(mport, mbox); 300 mport->inb_msg[mbox].mcback = NULL; 301 302 rc = release_resource(mport->inb_msg[mbox].res); 303 if (rc) 304 return rc; 305 306 kfree(mport->inb_msg[mbox].res); 307 mport->inb_msg[mbox].res = NULL; 308 309 return 0; 310 } 311 312 /** 313 * rio_request_outb_mbox - request outbound mailbox service 314 * @mport: RIO master port from which to allocate the mailbox resource 315 * @dev_id: Device specific pointer to pass on event 316 * @mbox: Mailbox number to claim 317 * @entries: Number of entries in outbound mailbox queue 318 * @moutb: Callback to execute when outbound message is sent 319 * 320 * Requests ownership of an outbound mailbox resource and binds 321 * a callback function to the resource. Returns 0 on success. 322 */ 323 int rio_request_outb_mbox(struct rio_mport *mport, 324 void *dev_id, 325 int mbox, 326 int entries, 327 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot)) 328 { 329 int rc = -ENOSYS; 330 struct resource *res; 331 332 if (mport->ops->open_outb_mbox == NULL) 333 goto out; 334 335 res = kzalloc(sizeof(struct resource), GFP_KERNEL); 336 337 if (res) { 338 rio_init_mbox_res(res, mbox, mbox); 339 340 /* Make sure this outbound mailbox isn't in use */ 341 if ((rc = 342 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 343 res)) < 0) { 344 kfree(res); 345 goto out; 346 } 347 348 mport->outb_msg[mbox].res = res; 349 350 /* Hook the inbound message callback */ 351 mport->outb_msg[mbox].mcback = moutb; 352 353 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries); 354 if (rc) { 355 mport->outb_msg[mbox].mcback = NULL; 356 mport->outb_msg[mbox].res = NULL; 357 release_resource(res); 358 kfree(res); 359 } 360 } else 361 rc = -ENOMEM; 362 363 out: 364 return rc; 365 } 366 367 /** 368 * rio_release_outb_mbox - release outbound mailbox message service 369 * @mport: RIO master port from which to release the mailbox resource 370 * @mbox: Mailbox number to release 371 * 372 * Releases ownership of an inbound mailbox resource. Returns 0 373 * if the request has been satisfied. 374 */ 375 int rio_release_outb_mbox(struct rio_mport *mport, int mbox) 376 { 377 int rc; 378 379 if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res) 380 return -EINVAL; 381 382 mport->ops->close_outb_mbox(mport, mbox); 383 mport->outb_msg[mbox].mcback = NULL; 384 385 rc = release_resource(mport->outb_msg[mbox].res); 386 if (rc) 387 return rc; 388 389 kfree(mport->outb_msg[mbox].res); 390 mport->outb_msg[mbox].res = NULL; 391 392 return 0; 393 } 394 395 /** 396 * rio_setup_inb_dbell - bind inbound doorbell callback 397 * @mport: RIO master port to bind the doorbell callback 398 * @dev_id: Device specific pointer to pass on event 399 * @res: Doorbell message resource 400 * @dinb: Callback to execute when doorbell is received 401 * 402 * Adds a doorbell resource/callback pair into a port's 403 * doorbell event list. Returns 0 if the request has been 404 * satisfied. 405 */ 406 static int 407 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res, 408 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst, 409 u16 info)) 410 { 411 int rc = 0; 412 struct rio_dbell *dbell; 413 414 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) { 415 rc = -ENOMEM; 416 goto out; 417 } 418 419 dbell->res = res; 420 dbell->dinb = dinb; 421 dbell->dev_id = dev_id; 422 423 mutex_lock(&mport->lock); 424 list_add_tail(&dbell->node, &mport->dbells); 425 mutex_unlock(&mport->lock); 426 427 out: 428 return rc; 429 } 430 431 /** 432 * rio_request_inb_dbell - request inbound doorbell message service 433 * @mport: RIO master port from which to allocate the doorbell resource 434 * @dev_id: Device specific pointer to pass on event 435 * @start: Doorbell info range start 436 * @end: Doorbell info range end 437 * @dinb: Callback to execute when doorbell is received 438 * 439 * Requests ownership of an inbound doorbell resource and binds 440 * a callback function to the resource. Returns 0 if the request 441 * has been satisfied. 442 */ 443 int rio_request_inb_dbell(struct rio_mport *mport, 444 void *dev_id, 445 u16 start, 446 u16 end, 447 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, 448 u16 dst, u16 info)) 449 { 450 int rc = 0; 451 452 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL); 453 454 if (res) { 455 rio_init_dbell_res(res, start, end); 456 457 /* Make sure these doorbells aren't in use */ 458 if ((rc = 459 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE], 460 res)) < 0) { 461 kfree(res); 462 goto out; 463 } 464 465 /* Hook the doorbell callback */ 466 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb); 467 } else 468 rc = -ENOMEM; 469 470 out: 471 return rc; 472 } 473 474 /** 475 * rio_release_inb_dbell - release inbound doorbell message service 476 * @mport: RIO master port from which to release the doorbell resource 477 * @start: Doorbell info range start 478 * @end: Doorbell info range end 479 * 480 * Releases ownership of an inbound doorbell resource and removes 481 * callback from the doorbell event list. Returns 0 if the request 482 * has been satisfied. 483 */ 484 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end) 485 { 486 int rc = 0, found = 0; 487 struct rio_dbell *dbell; 488 489 mutex_lock(&mport->lock); 490 list_for_each_entry(dbell, &mport->dbells, node) { 491 if ((dbell->res->start == start) && (dbell->res->end == end)) { 492 list_del(&dbell->node); 493 found = 1; 494 break; 495 } 496 } 497 mutex_unlock(&mport->lock); 498 499 /* If we can't find an exact match, fail */ 500 if (!found) { 501 rc = -EINVAL; 502 goto out; 503 } 504 505 /* Release the doorbell resource */ 506 rc = release_resource(dbell->res); 507 508 /* Free the doorbell event */ 509 kfree(dbell); 510 511 out: 512 return rc; 513 } 514 515 /** 516 * rio_request_outb_dbell - request outbound doorbell message range 517 * @rdev: RIO device from which to allocate the doorbell resource 518 * @start: Doorbell message range start 519 * @end: Doorbell message range end 520 * 521 * Requests ownership of a doorbell message range. Returns a resource 522 * if the request has been satisfied or %NULL on failure. 523 */ 524 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start, 525 u16 end) 526 { 527 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL); 528 529 if (res) { 530 rio_init_dbell_res(res, start, end); 531 532 /* Make sure these doorbells aren't in use */ 533 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res) 534 < 0) { 535 kfree(res); 536 res = NULL; 537 } 538 } 539 540 return res; 541 } 542 543 /** 544 * rio_release_outb_dbell - release outbound doorbell message range 545 * @rdev: RIO device from which to release the doorbell resource 546 * @res: Doorbell resource to be freed 547 * 548 * Releases ownership of a doorbell message range. Returns 0 if the 549 * request has been satisfied. 550 */ 551 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res) 552 { 553 int rc = release_resource(res); 554 555 kfree(res); 556 557 return rc; 558 } 559 560 /** 561 * rio_add_mport_pw_handler - add port-write message handler into the list 562 * of mport specific pw handlers 563 * @mport: RIO master port to bind the portwrite callback 564 * @context: Handler specific context to pass on event 565 * @pwcback: Callback to execute when portwrite is received 566 * 567 * Returns 0 if the request has been satisfied. 568 */ 569 int rio_add_mport_pw_handler(struct rio_mport *mport, void *context, 570 int (*pwcback)(struct rio_mport *mport, 571 void *context, union rio_pw_msg *msg, int step)) 572 { 573 int rc = 0; 574 struct rio_pwrite *pwrite; 575 576 pwrite = kzalloc(sizeof(struct rio_pwrite), GFP_KERNEL); 577 if (!pwrite) { 578 rc = -ENOMEM; 579 goto out; 580 } 581 582 pwrite->pwcback = pwcback; 583 pwrite->context = context; 584 mutex_lock(&mport->lock); 585 list_add_tail(&pwrite->node, &mport->pwrites); 586 mutex_unlock(&mport->lock); 587 out: 588 return rc; 589 } 590 EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler); 591 592 /** 593 * rio_del_mport_pw_handler - remove port-write message handler from the list 594 * of mport specific pw handlers 595 * @mport: RIO master port to bind the portwrite callback 596 * @context: Registered handler specific context to pass on event 597 * @pwcback: Registered callback function 598 * 599 * Returns 0 if the request has been satisfied. 600 */ 601 int rio_del_mport_pw_handler(struct rio_mport *mport, void *context, 602 int (*pwcback)(struct rio_mport *mport, 603 void *context, union rio_pw_msg *msg, int step)) 604 { 605 int rc = -EINVAL; 606 struct rio_pwrite *pwrite; 607 608 mutex_lock(&mport->lock); 609 list_for_each_entry(pwrite, &mport->pwrites, node) { 610 if (pwrite->pwcback == pwcback && pwrite->context == context) { 611 list_del(&pwrite->node); 612 kfree(pwrite); 613 rc = 0; 614 break; 615 } 616 } 617 mutex_unlock(&mport->lock); 618 619 return rc; 620 } 621 EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler); 622 623 /** 624 * rio_request_inb_pwrite - request inbound port-write message service for 625 * specific RapidIO device 626 * @rdev: RIO device to which register inbound port-write callback routine 627 * @pwcback: Callback routine to execute when port-write is received 628 * 629 * Binds a port-write callback function to the RapidIO device. 630 * Returns 0 if the request has been satisfied. 631 */ 632 int rio_request_inb_pwrite(struct rio_dev *rdev, 633 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step)) 634 { 635 int rc = 0; 636 637 spin_lock(&rio_global_list_lock); 638 if (rdev->pwcback != NULL) 639 rc = -ENOMEM; 640 else 641 rdev->pwcback = pwcback; 642 643 spin_unlock(&rio_global_list_lock); 644 return rc; 645 } 646 EXPORT_SYMBOL_GPL(rio_request_inb_pwrite); 647 648 /** 649 * rio_release_inb_pwrite - release inbound port-write message service 650 * associated with specific RapidIO device 651 * @rdev: RIO device which registered for inbound port-write callback 652 * 653 * Removes callback from the rio_dev structure. Returns 0 if the request 654 * has been satisfied. 655 */ 656 int rio_release_inb_pwrite(struct rio_dev *rdev) 657 { 658 int rc = -ENOMEM; 659 660 spin_lock(&rio_global_list_lock); 661 if (rdev->pwcback) { 662 rdev->pwcback = NULL; 663 rc = 0; 664 } 665 666 spin_unlock(&rio_global_list_lock); 667 return rc; 668 } 669 EXPORT_SYMBOL_GPL(rio_release_inb_pwrite); 670 671 /** 672 * rio_pw_enable - Enables/disables port-write handling by a master port 673 * @mport: Master port associated with port-write handling 674 * @enable: 1=enable, 0=disable 675 */ 676 void rio_pw_enable(struct rio_mport *mport, int enable) 677 { 678 if (mport->ops->pwenable) { 679 mutex_lock(&mport->lock); 680 681 if ((enable && ++mport->pwe_refcnt == 1) || 682 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0)) 683 mport->ops->pwenable(mport, enable); 684 mutex_unlock(&mport->lock); 685 } 686 } 687 EXPORT_SYMBOL_GPL(rio_pw_enable); 688 689 /** 690 * rio_map_inb_region -- Map inbound memory region. 691 * @mport: Master port. 692 * @local: physical address of memory region to be mapped 693 * @rbase: RIO base address assigned to this window 694 * @size: Size of the memory region 695 * @rflags: Flags for mapping. 696 * 697 * Return: 0 -- Success. 698 * 699 * This function will create the mapping from RIO space to local memory. 700 */ 701 int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local, 702 u64 rbase, u32 size, u32 rflags) 703 { 704 int rc = 0; 705 unsigned long flags; 706 707 if (!mport->ops->map_inb) 708 return -1; 709 spin_lock_irqsave(&rio_mmap_lock, flags); 710 rc = mport->ops->map_inb(mport, local, rbase, size, rflags); 711 spin_unlock_irqrestore(&rio_mmap_lock, flags); 712 return rc; 713 } 714 EXPORT_SYMBOL_GPL(rio_map_inb_region); 715 716 /** 717 * rio_unmap_inb_region -- Unmap the inbound memory region 718 * @mport: Master port 719 * @lstart: physical address of memory region to be unmapped 720 */ 721 void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart) 722 { 723 unsigned long flags; 724 if (!mport->ops->unmap_inb) 725 return; 726 spin_lock_irqsave(&rio_mmap_lock, flags); 727 mport->ops->unmap_inb(mport, lstart); 728 spin_unlock_irqrestore(&rio_mmap_lock, flags); 729 } 730 EXPORT_SYMBOL_GPL(rio_unmap_inb_region); 731 732 /** 733 * rio_map_outb_region -- Map outbound memory region. 734 * @mport: Master port. 735 * @destid: destination id window points to 736 * @rbase: RIO base address window translates to 737 * @size: Size of the memory region 738 * @rflags: Flags for mapping. 739 * @local: physical address of memory region mapped 740 * 741 * Return: 0 -- Success. 742 * 743 * This function will create the mapping from RIO space to local memory. 744 */ 745 int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase, 746 u32 size, u32 rflags, dma_addr_t *local) 747 { 748 int rc = 0; 749 unsigned long flags; 750 751 if (!mport->ops->map_outb) 752 return -ENODEV; 753 754 spin_lock_irqsave(&rio_mmap_lock, flags); 755 rc = mport->ops->map_outb(mport, destid, rbase, size, 756 rflags, local); 757 spin_unlock_irqrestore(&rio_mmap_lock, flags); 758 759 return rc; 760 } 761 EXPORT_SYMBOL_GPL(rio_map_outb_region); 762 763 /** 764 * rio_unmap_inb_region -- Unmap the inbound memory region 765 * @mport: Master port 766 * @destid: destination id mapping points to 767 * @rstart: RIO base address window translates to 768 */ 769 void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart) 770 { 771 unsigned long flags; 772 773 if (!mport->ops->unmap_outb) 774 return; 775 776 spin_lock_irqsave(&rio_mmap_lock, flags); 777 mport->ops->unmap_outb(mport, destid, rstart); 778 spin_unlock_irqrestore(&rio_mmap_lock, flags); 779 } 780 EXPORT_SYMBOL_GPL(rio_unmap_outb_region); 781 782 /** 783 * rio_mport_get_physefb - Helper function that returns register offset 784 * for Physical Layer Extended Features Block. 785 * @port: Master port to issue transaction 786 * @local: Indicate a local master port or remote device access 787 * @destid: Destination ID of the device 788 * @hopcount: Number of switch hops to the device 789 * @rmap: pointer to location to store register map type info 790 */ 791 u32 792 rio_mport_get_physefb(struct rio_mport *port, int local, 793 u16 destid, u8 hopcount, u32 *rmap) 794 { 795 u32 ext_ftr_ptr; 796 u32 ftr_header; 797 798 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0); 799 800 while (ext_ftr_ptr) { 801 if (local) 802 rio_local_read_config_32(port, ext_ftr_ptr, 803 &ftr_header); 804 else 805 rio_mport_read_config_32(port, destid, hopcount, 806 ext_ftr_ptr, &ftr_header); 807 808 ftr_header = RIO_GET_BLOCK_ID(ftr_header); 809 switch (ftr_header) { 810 811 case RIO_EFB_SER_EP_ID: 812 case RIO_EFB_SER_EP_REC_ID: 813 case RIO_EFB_SER_EP_FREE_ID: 814 case RIO_EFB_SER_EP_M1_ID: 815 case RIO_EFB_SER_EP_SW_M1_ID: 816 case RIO_EFB_SER_EPF_M1_ID: 817 case RIO_EFB_SER_EPF_SW_M1_ID: 818 *rmap = 1; 819 return ext_ftr_ptr; 820 821 case RIO_EFB_SER_EP_M2_ID: 822 case RIO_EFB_SER_EP_SW_M2_ID: 823 case RIO_EFB_SER_EPF_M2_ID: 824 case RIO_EFB_SER_EPF_SW_M2_ID: 825 *rmap = 2; 826 return ext_ftr_ptr; 827 828 default: 829 break; 830 } 831 832 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, 833 hopcount, ext_ftr_ptr); 834 } 835 836 return ext_ftr_ptr; 837 } 838 EXPORT_SYMBOL_GPL(rio_mport_get_physefb); 839 840 /** 841 * rio_get_comptag - Begin or continue searching for a RIO device by component tag 842 * @comp_tag: RIO component tag to match 843 * @from: Previous RIO device found in search, or %NULL for new search 844 * 845 * Iterates through the list of known RIO devices. If a RIO device is 846 * found with a matching @comp_tag, a pointer to its device 847 * structure is returned. Otherwise, %NULL is returned. A new search 848 * is initiated by passing %NULL to the @from argument. Otherwise, if 849 * @from is not %NULL, searches continue from next device on the global 850 * list. 851 */ 852 struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from) 853 { 854 struct list_head *n; 855 struct rio_dev *rdev; 856 857 spin_lock(&rio_global_list_lock); 858 n = from ? from->global_list.next : rio_devices.next; 859 860 while (n && (n != &rio_devices)) { 861 rdev = rio_dev_g(n); 862 if (rdev->comp_tag == comp_tag) 863 goto exit; 864 n = n->next; 865 } 866 rdev = NULL; 867 exit: 868 spin_unlock(&rio_global_list_lock); 869 return rdev; 870 } 871 EXPORT_SYMBOL_GPL(rio_get_comptag); 872 873 /** 874 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port. 875 * @rdev: Pointer to RIO device control structure 876 * @pnum: Switch port number to set LOCKOUT bit 877 * @lock: Operation : set (=1) or clear (=0) 878 */ 879 int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock) 880 { 881 u32 regval; 882 883 rio_read_config_32(rdev, 884 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum), 885 ®val); 886 if (lock) 887 regval |= RIO_PORT_N_CTL_LOCKOUT; 888 else 889 regval &= ~RIO_PORT_N_CTL_LOCKOUT; 890 891 rio_write_config_32(rdev, 892 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum), 893 regval); 894 return 0; 895 } 896 EXPORT_SYMBOL_GPL(rio_set_port_lockout); 897 898 /** 899 * rio_enable_rx_tx_port - enable input receiver and output transmitter of 900 * given port 901 * @port: Master port associated with the RIO network 902 * @local: local=1 select local port otherwise a far device is reached 903 * @destid: Destination ID of the device to check host bit 904 * @hopcount: Number of hops to reach the target 905 * @port_num: Port (-number on switch) to enable on a far end device 906 * 907 * Returns 0 or 1 from on General Control Command and Status Register 908 * (EXT_PTR+0x3C) 909 */ 910 int rio_enable_rx_tx_port(struct rio_mport *port, 911 int local, u16 destid, 912 u8 hopcount, u8 port_num) 913 { 914 #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS 915 u32 regval; 916 u32 ext_ftr_ptr; 917 u32 rmap; 918 919 /* 920 * enable rx input tx output port 921 */ 922 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = " 923 "%d, port_num = %d)\n", local, destid, hopcount, port_num); 924 925 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, 926 hopcount, &rmap); 927 928 if (local) { 929 rio_local_read_config_32(port, 930 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), 931 ®val); 932 } else { 933 if (rio_mport_read_config_32(port, destid, hopcount, 934 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap), 935 ®val) < 0) 936 return -EIO; 937 } 938 939 regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX; 940 941 if (local) { 942 rio_local_write_config_32(port, 943 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval); 944 } else { 945 if (rio_mport_write_config_32(port, destid, hopcount, 946 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap), 947 regval) < 0) 948 return -EIO; 949 } 950 #endif 951 return 0; 952 } 953 EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port); 954 955 956 /** 957 * rio_chk_dev_route - Validate route to the specified device. 958 * @rdev: RIO device failed to respond 959 * @nrdev: Last active device on the route to rdev 960 * @npnum: nrdev's port number on the route to rdev 961 * 962 * Follows a route to the specified RIO device to determine the last available 963 * device (and corresponding RIO port) on the route. 964 */ 965 static int 966 rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum) 967 { 968 u32 result; 969 int p_port, rc = -EIO; 970 struct rio_dev *prev = NULL; 971 972 /* Find switch with failed RIO link */ 973 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) { 974 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) { 975 prev = rdev->prev; 976 break; 977 } 978 rdev = rdev->prev; 979 } 980 981 if (prev == NULL) 982 goto err_out; 983 984 p_port = prev->rswitch->route_table[rdev->destid]; 985 986 if (p_port != RIO_INVALID_ROUTE) { 987 pr_debug("RIO: link failed on [%s]-P%d\n", 988 rio_name(prev), p_port); 989 *nrdev = prev; 990 *npnum = p_port; 991 rc = 0; 992 } else 993 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev)); 994 err_out: 995 return rc; 996 } 997 998 /** 999 * rio_mport_chk_dev_access - Validate access to the specified device. 1000 * @mport: Master port to send transactions 1001 * @destid: Device destination ID in network 1002 * @hopcount: Number of hops into the network 1003 */ 1004 int 1005 rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount) 1006 { 1007 int i = 0; 1008 u32 tmp; 1009 1010 while (rio_mport_read_config_32(mport, destid, hopcount, 1011 RIO_DEV_ID_CAR, &tmp)) { 1012 i++; 1013 if (i == RIO_MAX_CHK_RETRY) 1014 return -EIO; 1015 mdelay(1); 1016 } 1017 1018 return 0; 1019 } 1020 EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access); 1021 1022 /** 1023 * rio_chk_dev_access - Validate access to the specified device. 1024 * @rdev: Pointer to RIO device control structure 1025 */ 1026 static int rio_chk_dev_access(struct rio_dev *rdev) 1027 { 1028 return rio_mport_chk_dev_access(rdev->net->hport, 1029 rdev->destid, rdev->hopcount); 1030 } 1031 1032 /** 1033 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and 1034 * returns link-response (if requested). 1035 * @rdev: RIO devive to issue Input-status command 1036 * @pnum: Device port number to issue the command 1037 * @lnkresp: Response from a link partner 1038 */ 1039 static int 1040 rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp) 1041 { 1042 u32 regval; 1043 int checkcount; 1044 1045 if (lnkresp) { 1046 /* Read from link maintenance response register 1047 * to clear valid bit */ 1048 rio_read_config_32(rdev, 1049 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum), 1050 ®val); 1051 udelay(50); 1052 } 1053 1054 /* Issue Input-status command */ 1055 rio_write_config_32(rdev, 1056 RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum), 1057 RIO_MNT_REQ_CMD_IS); 1058 1059 /* Exit if the response is not expected */ 1060 if (lnkresp == NULL) 1061 return 0; 1062 1063 checkcount = 3; 1064 while (checkcount--) { 1065 udelay(50); 1066 rio_read_config_32(rdev, 1067 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum), 1068 ®val); 1069 if (regval & RIO_PORT_N_MNT_RSP_RVAL) { 1070 *lnkresp = regval; 1071 return 0; 1072 } 1073 } 1074 1075 return -EIO; 1076 } 1077 1078 /** 1079 * rio_clr_err_stopped - Clears port Error-stopped states. 1080 * @rdev: Pointer to RIO device control structure 1081 * @pnum: Switch port number to clear errors 1082 * @err_status: port error status (if 0 reads register from device) 1083 * 1084 * TODO: Currently this routine is not compatible with recovery process 1085 * specified for idt_gen3 RapidIO switch devices. It has to be reviewed 1086 * to implement universal recovery process that is compatible full range 1087 * off available devices. 1088 * IDT gen3 switch driver now implements HW-specific error handler that 1089 * issues soft port reset to the port to reset ERR_STOP bits and ackIDs. 1090 */ 1091 static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status) 1092 { 1093 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum]; 1094 u32 regval; 1095 u32 far_ackid, far_linkstat, near_ackid; 1096 1097 if (err_status == 0) 1098 rio_read_config_32(rdev, 1099 RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum), 1100 &err_status); 1101 1102 if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) { 1103 pr_debug("RIO_EM: servicing Output Error-Stopped state\n"); 1104 /* 1105 * Send a Link-Request/Input-Status control symbol 1106 */ 1107 if (rio_get_input_status(rdev, pnum, ®val)) { 1108 pr_debug("RIO_EM: Input-status response timeout\n"); 1109 goto rd_err; 1110 } 1111 1112 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n", 1113 pnum, regval); 1114 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5; 1115 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT; 1116 rio_read_config_32(rdev, 1117 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum), 1118 ®val); 1119 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval); 1120 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24; 1121 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \ 1122 " near_ackID=0x%02x\n", 1123 pnum, far_ackid, far_linkstat, near_ackid); 1124 1125 /* 1126 * If required, synchronize ackIDs of near and 1127 * far sides. 1128 */ 1129 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) || 1130 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) { 1131 /* Align near outstanding/outbound ackIDs with 1132 * far inbound. 1133 */ 1134 rio_write_config_32(rdev, 1135 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum), 1136 (near_ackid << 24) | 1137 (far_ackid << 8) | far_ackid); 1138 /* Align far outstanding/outbound ackIDs with 1139 * near inbound. 1140 */ 1141 far_ackid++; 1142 if (!nextdev) { 1143 pr_debug("RIO_EM: nextdev pointer == NULL\n"); 1144 goto rd_err; 1145 } 1146 1147 rio_write_config_32(nextdev, 1148 RIO_DEV_PORT_N_ACK_STS_CSR(nextdev, 1149 RIO_GET_PORT_NUM(nextdev->swpinfo)), 1150 (far_ackid << 24) | 1151 (near_ackid << 8) | near_ackid); 1152 } 1153 rd_err: 1154 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum), 1155 &err_status); 1156 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status); 1157 } 1158 1159 if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) { 1160 pr_debug("RIO_EM: servicing Input Error-Stopped state\n"); 1161 rio_get_input_status(nextdev, 1162 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL); 1163 udelay(50); 1164 1165 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum), 1166 &err_status); 1167 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status); 1168 } 1169 1170 return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES | 1171 RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0; 1172 } 1173 1174 /** 1175 * rio_inb_pwrite_handler - inbound port-write message handler 1176 * @mport: mport device associated with port-write 1177 * @pw_msg: pointer to inbound port-write message 1178 * 1179 * Processes an inbound port-write message. Returns 0 if the request 1180 * has been satisfied. 1181 */ 1182 int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg) 1183 { 1184 struct rio_dev *rdev; 1185 u32 err_status, em_perrdet, em_ltlerrdet; 1186 int rc, portnum; 1187 struct rio_pwrite *pwrite; 1188 1189 #ifdef DEBUG_PW 1190 { 1191 u32 i; 1192 1193 pr_debug("%s: PW to mport_%d:\n", __func__, mport->id); 1194 for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) { 1195 pr_debug("0x%02x: %08x %08x %08x %08x\n", 1196 i * 4, pw_msg->raw[i], pw_msg->raw[i + 1], 1197 pw_msg->raw[i + 2], pw_msg->raw[i + 3]); 1198 } 1199 } 1200 #endif 1201 1202 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL); 1203 if (rdev) { 1204 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev)); 1205 } else { 1206 pr_debug("RIO: %s No matching device for CTag 0x%08x\n", 1207 __func__, pw_msg->em.comptag); 1208 } 1209 1210 /* Call a device-specific handler (if it is registered for the device). 1211 * This may be the service for endpoints that send device-specific 1212 * port-write messages. End-point messages expected to be handled 1213 * completely by EP specific device driver. 1214 * For switches rc==0 signals that no standard processing required. 1215 */ 1216 if (rdev && rdev->pwcback) { 1217 rc = rdev->pwcback(rdev, pw_msg, 0); 1218 if (rc == 0) 1219 return 0; 1220 } 1221 1222 mutex_lock(&mport->lock); 1223 list_for_each_entry(pwrite, &mport->pwrites, node) 1224 pwrite->pwcback(mport, pwrite->context, pw_msg, 0); 1225 mutex_unlock(&mport->lock); 1226 1227 if (!rdev) 1228 return 0; 1229 1230 /* 1231 * FIXME: The code below stays as it was before for now until we decide 1232 * how to do default PW handling in combination with per-mport callbacks 1233 */ 1234 1235 portnum = pw_msg->em.is_port & 0xFF; 1236 1237 /* Check if device and route to it are functional: 1238 * Sometimes devices may send PW message(s) just before being 1239 * powered down (or link being lost). 1240 */ 1241 if (rio_chk_dev_access(rdev)) { 1242 pr_debug("RIO: device access failed - get link partner\n"); 1243 /* Scan route to the device and identify failed link. 1244 * This will replace device and port reported in PW message. 1245 * PW message should not be used after this point. 1246 */ 1247 if (rio_chk_dev_route(rdev, &rdev, &portnum)) { 1248 pr_err("RIO: Route trace for %s failed\n", 1249 rio_name(rdev)); 1250 return -EIO; 1251 } 1252 pw_msg = NULL; 1253 } 1254 1255 /* For End-point devices processing stops here */ 1256 if (!(rdev->pef & RIO_PEF_SWITCH)) 1257 return 0; 1258 1259 if (rdev->phys_efptr == 0) { 1260 pr_err("RIO_PW: Bad switch initialization for %s\n", 1261 rio_name(rdev)); 1262 return 0; 1263 } 1264 1265 /* 1266 * Process the port-write notification from switch 1267 */ 1268 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle) 1269 rdev->rswitch->ops->em_handle(rdev, portnum); 1270 1271 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum), 1272 &err_status); 1273 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status); 1274 1275 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) { 1276 1277 if (!(rdev->rswitch->port_ok & (1 << portnum))) { 1278 rdev->rswitch->port_ok |= (1 << portnum); 1279 rio_set_port_lockout(rdev, portnum, 0); 1280 /* Schedule Insertion Service */ 1281 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n", 1282 rio_name(rdev), portnum); 1283 } 1284 1285 /* Clear error-stopped states (if reported). 1286 * Depending on the link partner state, two attempts 1287 * may be needed for successful recovery. 1288 */ 1289 if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES | 1290 RIO_PORT_N_ERR_STS_INP_ES)) { 1291 if (rio_clr_err_stopped(rdev, portnum, err_status)) 1292 rio_clr_err_stopped(rdev, portnum, 0); 1293 } 1294 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */ 1295 1296 if (rdev->rswitch->port_ok & (1 << portnum)) { 1297 rdev->rswitch->port_ok &= ~(1 << portnum); 1298 rio_set_port_lockout(rdev, portnum, 1); 1299 1300 if (rdev->phys_rmap == 1) { 1301 rio_write_config_32(rdev, 1302 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum), 1303 RIO_PORT_N_ACK_CLEAR); 1304 } else { 1305 rio_write_config_32(rdev, 1306 RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum), 1307 RIO_PORT_N_OB_ACK_CLEAR); 1308 rio_write_config_32(rdev, 1309 RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum), 1310 0); 1311 } 1312 1313 /* Schedule Extraction Service */ 1314 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n", 1315 rio_name(rdev), portnum); 1316 } 1317 } 1318 1319 rio_read_config_32(rdev, 1320 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet); 1321 if (em_perrdet) { 1322 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n", 1323 portnum, em_perrdet); 1324 /* Clear EM Port N Error Detect CSR */ 1325 rio_write_config_32(rdev, 1326 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0); 1327 } 1328 1329 rio_read_config_32(rdev, 1330 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet); 1331 if (em_ltlerrdet) { 1332 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n", 1333 em_ltlerrdet); 1334 /* Clear EM L/T Layer Error Detect CSR */ 1335 rio_write_config_32(rdev, 1336 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0); 1337 } 1338 1339 /* Clear remaining error bits and Port-Write Pending bit */ 1340 rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum), 1341 err_status); 1342 1343 return 0; 1344 } 1345 EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler); 1346 1347 /** 1348 * rio_mport_get_efb - get pointer to next extended features block 1349 * @port: Master port to issue transaction 1350 * @local: Indicate a local master port or remote device access 1351 * @destid: Destination ID of the device 1352 * @hopcount: Number of switch hops to the device 1353 * @from: Offset of current Extended Feature block header (if 0 starts 1354 * from ExtFeaturePtr) 1355 */ 1356 u32 1357 rio_mport_get_efb(struct rio_mport *port, int local, u16 destid, 1358 u8 hopcount, u32 from) 1359 { 1360 u32 reg_val; 1361 1362 if (from == 0) { 1363 if (local) 1364 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, 1365 ®_val); 1366 else 1367 rio_mport_read_config_32(port, destid, hopcount, 1368 RIO_ASM_INFO_CAR, ®_val); 1369 return reg_val & RIO_EXT_FTR_PTR_MASK; 1370 } else { 1371 if (local) 1372 rio_local_read_config_32(port, from, ®_val); 1373 else 1374 rio_mport_read_config_32(port, destid, hopcount, 1375 from, ®_val); 1376 return RIO_GET_BLOCK_ID(reg_val); 1377 } 1378 } 1379 EXPORT_SYMBOL_GPL(rio_mport_get_efb); 1380 1381 /** 1382 * rio_mport_get_feature - query for devices' extended features 1383 * @port: Master port to issue transaction 1384 * @local: Indicate a local master port or remote device access 1385 * @destid: Destination ID of the device 1386 * @hopcount: Number of switch hops to the device 1387 * @ftr: Extended feature code 1388 * 1389 * Tell if a device supports a given RapidIO capability. 1390 * Returns the offset of the requested extended feature 1391 * block within the device's RIO configuration space or 1392 * 0 in case the device does not support it. 1393 */ 1394 u32 1395 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid, 1396 u8 hopcount, int ftr) 1397 { 1398 u32 asm_info, ext_ftr_ptr, ftr_header; 1399 1400 if (local) 1401 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info); 1402 else 1403 rio_mport_read_config_32(port, destid, hopcount, 1404 RIO_ASM_INFO_CAR, &asm_info); 1405 1406 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK; 1407 1408 while (ext_ftr_ptr) { 1409 if (local) 1410 rio_local_read_config_32(port, ext_ftr_ptr, 1411 &ftr_header); 1412 else 1413 rio_mport_read_config_32(port, destid, hopcount, 1414 ext_ftr_ptr, &ftr_header); 1415 if (RIO_GET_BLOCK_ID(ftr_header) == ftr) 1416 return ext_ftr_ptr; 1417 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header))) 1418 break; 1419 } 1420 1421 return 0; 1422 } 1423 EXPORT_SYMBOL_GPL(rio_mport_get_feature); 1424 1425 /** 1426 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did 1427 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids 1428 * @did: RIO did to match or %RIO_ANY_ID to match all dids 1429 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids 1430 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids 1431 * @from: Previous RIO device found in search, or %NULL for new search 1432 * 1433 * Iterates through the list of known RIO devices. If a RIO device is 1434 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference 1435 * count to the device is incrememted and a pointer to its device 1436 * structure is returned. Otherwise, %NULL is returned. A new search 1437 * is initiated by passing %NULL to the @from argument. Otherwise, if 1438 * @from is not %NULL, searches continue from next device on the global 1439 * list. The reference count for @from is always decremented if it is 1440 * not %NULL. 1441 */ 1442 struct rio_dev *rio_get_asm(u16 vid, u16 did, 1443 u16 asm_vid, u16 asm_did, struct rio_dev *from) 1444 { 1445 struct list_head *n; 1446 struct rio_dev *rdev; 1447 1448 WARN_ON(in_interrupt()); 1449 spin_lock(&rio_global_list_lock); 1450 n = from ? from->global_list.next : rio_devices.next; 1451 1452 while (n && (n != &rio_devices)) { 1453 rdev = rio_dev_g(n); 1454 if ((vid == RIO_ANY_ID || rdev->vid == vid) && 1455 (did == RIO_ANY_ID || rdev->did == did) && 1456 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) && 1457 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did)) 1458 goto exit; 1459 n = n->next; 1460 } 1461 rdev = NULL; 1462 exit: 1463 rio_dev_put(from); 1464 rdev = rio_dev_get(rdev); 1465 spin_unlock(&rio_global_list_lock); 1466 return rdev; 1467 } 1468 1469 /** 1470 * rio_get_device - Begin or continue searching for a RIO device by vid/did 1471 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids 1472 * @did: RIO did to match or %RIO_ANY_ID to match all dids 1473 * @from: Previous RIO device found in search, or %NULL for new search 1474 * 1475 * Iterates through the list of known RIO devices. If a RIO device is 1476 * found with a matching @vid and @did, the reference count to the 1477 * device is incrememted and a pointer to its device structure is returned. 1478 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL 1479 * to the @from argument. Otherwise, if @from is not %NULL, searches 1480 * continue from next device on the global list. The reference count for 1481 * @from is always decremented if it is not %NULL. 1482 */ 1483 struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from) 1484 { 1485 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from); 1486 } 1487 1488 /** 1489 * rio_std_route_add_entry - Add switch route table entry using standard 1490 * registers defined in RIO specification rev.1.3 1491 * @mport: Master port to issue transaction 1492 * @destid: Destination ID of the device 1493 * @hopcount: Number of switch hops to the device 1494 * @table: routing table ID (global or port-specific) 1495 * @route_destid: destID entry in the RT 1496 * @route_port: destination port for specified destID 1497 */ 1498 static int 1499 rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount, 1500 u16 table, u16 route_destid, u8 route_port) 1501 { 1502 if (table == RIO_GLOBAL_TABLE) { 1503 rio_mport_write_config_32(mport, destid, hopcount, 1504 RIO_STD_RTE_CONF_DESTID_SEL_CSR, 1505 (u32)route_destid); 1506 rio_mport_write_config_32(mport, destid, hopcount, 1507 RIO_STD_RTE_CONF_PORT_SEL_CSR, 1508 (u32)route_port); 1509 } 1510 1511 udelay(10); 1512 return 0; 1513 } 1514 1515 /** 1516 * rio_std_route_get_entry - Read switch route table entry (port number) 1517 * associated with specified destID using standard registers defined in RIO 1518 * specification rev.1.3 1519 * @mport: Master port to issue transaction 1520 * @destid: Destination ID of the device 1521 * @hopcount: Number of switch hops to the device 1522 * @table: routing table ID (global or port-specific) 1523 * @route_destid: destID entry in the RT 1524 * @route_port: returned destination port for specified destID 1525 */ 1526 static int 1527 rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount, 1528 u16 table, u16 route_destid, u8 *route_port) 1529 { 1530 u32 result; 1531 1532 if (table == RIO_GLOBAL_TABLE) { 1533 rio_mport_write_config_32(mport, destid, hopcount, 1534 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid); 1535 rio_mport_read_config_32(mport, destid, hopcount, 1536 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result); 1537 1538 *route_port = (u8)result; 1539 } 1540 1541 return 0; 1542 } 1543 1544 /** 1545 * rio_std_route_clr_table - Clear swotch route table using standard registers 1546 * defined in RIO specification rev.1.3. 1547 * @mport: Master port to issue transaction 1548 * @destid: Destination ID of the device 1549 * @hopcount: Number of switch hops to the device 1550 * @table: routing table ID (global or port-specific) 1551 */ 1552 static int 1553 rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount, 1554 u16 table) 1555 { 1556 u32 max_destid = 0xff; 1557 u32 i, pef, id_inc = 1, ext_cfg = 0; 1558 u32 port_sel = RIO_INVALID_ROUTE; 1559 1560 if (table == RIO_GLOBAL_TABLE) { 1561 rio_mport_read_config_32(mport, destid, hopcount, 1562 RIO_PEF_CAR, &pef); 1563 1564 if (mport->sys_size) { 1565 rio_mport_read_config_32(mport, destid, hopcount, 1566 RIO_SWITCH_RT_LIMIT, 1567 &max_destid); 1568 max_destid &= RIO_RT_MAX_DESTID; 1569 } 1570 1571 if (pef & RIO_PEF_EXT_RT) { 1572 ext_cfg = 0x80000000; 1573 id_inc = 4; 1574 port_sel = (RIO_INVALID_ROUTE << 24) | 1575 (RIO_INVALID_ROUTE << 16) | 1576 (RIO_INVALID_ROUTE << 8) | 1577 RIO_INVALID_ROUTE; 1578 } 1579 1580 for (i = 0; i <= max_destid;) { 1581 rio_mport_write_config_32(mport, destid, hopcount, 1582 RIO_STD_RTE_CONF_DESTID_SEL_CSR, 1583 ext_cfg | i); 1584 rio_mport_write_config_32(mport, destid, hopcount, 1585 RIO_STD_RTE_CONF_PORT_SEL_CSR, 1586 port_sel); 1587 i += id_inc; 1588 } 1589 } 1590 1591 udelay(10); 1592 return 0; 1593 } 1594 1595 /** 1596 * rio_lock_device - Acquires host device lock for specified device 1597 * @port: Master port to send transaction 1598 * @destid: Destination ID for device/switch 1599 * @hopcount: Hopcount to reach switch 1600 * @wait_ms: Max wait time in msec (0 = no timeout) 1601 * 1602 * Attepts to acquire host device lock for specified device 1603 * Returns 0 if device lock acquired or EINVAL if timeout expires. 1604 */ 1605 int rio_lock_device(struct rio_mport *port, u16 destid, 1606 u8 hopcount, int wait_ms) 1607 { 1608 u32 result; 1609 int tcnt = 0; 1610 1611 /* Attempt to acquire device lock */ 1612 rio_mport_write_config_32(port, destid, hopcount, 1613 RIO_HOST_DID_LOCK_CSR, port->host_deviceid); 1614 rio_mport_read_config_32(port, destid, hopcount, 1615 RIO_HOST_DID_LOCK_CSR, &result); 1616 1617 while (result != port->host_deviceid) { 1618 if (wait_ms != 0 && tcnt == wait_ms) { 1619 pr_debug("RIO: timeout when locking device %x:%x\n", 1620 destid, hopcount); 1621 return -EINVAL; 1622 } 1623 1624 /* Delay a bit */ 1625 mdelay(1); 1626 tcnt++; 1627 /* Try to acquire device lock again */ 1628 rio_mport_write_config_32(port, destid, 1629 hopcount, 1630 RIO_HOST_DID_LOCK_CSR, 1631 port->host_deviceid); 1632 rio_mport_read_config_32(port, destid, 1633 hopcount, 1634 RIO_HOST_DID_LOCK_CSR, &result); 1635 } 1636 1637 return 0; 1638 } 1639 EXPORT_SYMBOL_GPL(rio_lock_device); 1640 1641 /** 1642 * rio_unlock_device - Releases host device lock for specified device 1643 * @port: Master port to send transaction 1644 * @destid: Destination ID for device/switch 1645 * @hopcount: Hopcount to reach switch 1646 * 1647 * Returns 0 if device lock released or EINVAL if fails. 1648 */ 1649 int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount) 1650 { 1651 u32 result; 1652 1653 /* Release device lock */ 1654 rio_mport_write_config_32(port, destid, 1655 hopcount, 1656 RIO_HOST_DID_LOCK_CSR, 1657 port->host_deviceid); 1658 rio_mport_read_config_32(port, destid, hopcount, 1659 RIO_HOST_DID_LOCK_CSR, &result); 1660 if ((result & 0xffff) != 0xffff) { 1661 pr_debug("RIO: badness when releasing device lock %x:%x\n", 1662 destid, hopcount); 1663 return -EINVAL; 1664 } 1665 1666 return 0; 1667 } 1668 EXPORT_SYMBOL_GPL(rio_unlock_device); 1669 1670 /** 1671 * rio_route_add_entry- Add a route entry to a switch routing table 1672 * @rdev: RIO device 1673 * @table: Routing table ID 1674 * @route_destid: Destination ID to be routed 1675 * @route_port: Port number to be routed 1676 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock) 1677 * 1678 * If available calls the switch specific add_entry() method to add a route 1679 * entry into a switch routing table. Otherwise uses standard RT update method 1680 * as defined by RapidIO specification. A specific routing table can be selected 1681 * using the @table argument if a switch has per port routing tables or 1682 * the standard (or global) table may be used by passing 1683 * %RIO_GLOBAL_TABLE in @table. 1684 * 1685 * Returns %0 on success or %-EINVAL on failure. 1686 */ 1687 int rio_route_add_entry(struct rio_dev *rdev, 1688 u16 table, u16 route_destid, u8 route_port, int lock) 1689 { 1690 int rc = -EINVAL; 1691 struct rio_switch_ops *ops = rdev->rswitch->ops; 1692 1693 if (lock) { 1694 rc = rio_lock_device(rdev->net->hport, rdev->destid, 1695 rdev->hopcount, 1000); 1696 if (rc) 1697 return rc; 1698 } 1699 1700 spin_lock(&rdev->rswitch->lock); 1701 1702 if (ops == NULL || ops->add_entry == NULL) { 1703 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid, 1704 rdev->hopcount, table, 1705 route_destid, route_port); 1706 } else if (try_module_get(ops->owner)) { 1707 rc = ops->add_entry(rdev->net->hport, rdev->destid, 1708 rdev->hopcount, table, route_destid, 1709 route_port); 1710 module_put(ops->owner); 1711 } 1712 1713 spin_unlock(&rdev->rswitch->lock); 1714 1715 if (lock) 1716 rio_unlock_device(rdev->net->hport, rdev->destid, 1717 rdev->hopcount); 1718 1719 return rc; 1720 } 1721 EXPORT_SYMBOL_GPL(rio_route_add_entry); 1722 1723 /** 1724 * rio_route_get_entry- Read an entry from a switch routing table 1725 * @rdev: RIO device 1726 * @table: Routing table ID 1727 * @route_destid: Destination ID to be routed 1728 * @route_port: Pointer to read port number into 1729 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock) 1730 * 1731 * If available calls the switch specific get_entry() method to fetch a route 1732 * entry from a switch routing table. Otherwise uses standard RT read method 1733 * as defined by RapidIO specification. A specific routing table can be selected 1734 * using the @table argument if a switch has per port routing tables or 1735 * the standard (or global) table may be used by passing 1736 * %RIO_GLOBAL_TABLE in @table. 1737 * 1738 * Returns %0 on success or %-EINVAL on failure. 1739 */ 1740 int rio_route_get_entry(struct rio_dev *rdev, u16 table, 1741 u16 route_destid, u8 *route_port, int lock) 1742 { 1743 int rc = -EINVAL; 1744 struct rio_switch_ops *ops = rdev->rswitch->ops; 1745 1746 if (lock) { 1747 rc = rio_lock_device(rdev->net->hport, rdev->destid, 1748 rdev->hopcount, 1000); 1749 if (rc) 1750 return rc; 1751 } 1752 1753 spin_lock(&rdev->rswitch->lock); 1754 1755 if (ops == NULL || ops->get_entry == NULL) { 1756 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid, 1757 rdev->hopcount, table, 1758 route_destid, route_port); 1759 } else if (try_module_get(ops->owner)) { 1760 rc = ops->get_entry(rdev->net->hport, rdev->destid, 1761 rdev->hopcount, table, route_destid, 1762 route_port); 1763 module_put(ops->owner); 1764 } 1765 1766 spin_unlock(&rdev->rswitch->lock); 1767 1768 if (lock) 1769 rio_unlock_device(rdev->net->hport, rdev->destid, 1770 rdev->hopcount); 1771 return rc; 1772 } 1773 EXPORT_SYMBOL_GPL(rio_route_get_entry); 1774 1775 /** 1776 * rio_route_clr_table - Clear a switch routing table 1777 * @rdev: RIO device 1778 * @table: Routing table ID 1779 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock) 1780 * 1781 * If available calls the switch specific clr_table() method to clear a switch 1782 * routing table. Otherwise uses standard RT write method as defined by RapidIO 1783 * specification. A specific routing table can be selected using the @table 1784 * argument if a switch has per port routing tables or the standard (or global) 1785 * table may be used by passing %RIO_GLOBAL_TABLE in @table. 1786 * 1787 * Returns %0 on success or %-EINVAL on failure. 1788 */ 1789 int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock) 1790 { 1791 int rc = -EINVAL; 1792 struct rio_switch_ops *ops = rdev->rswitch->ops; 1793 1794 if (lock) { 1795 rc = rio_lock_device(rdev->net->hport, rdev->destid, 1796 rdev->hopcount, 1000); 1797 if (rc) 1798 return rc; 1799 } 1800 1801 spin_lock(&rdev->rswitch->lock); 1802 1803 if (ops == NULL || ops->clr_table == NULL) { 1804 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid, 1805 rdev->hopcount, table); 1806 } else if (try_module_get(ops->owner)) { 1807 rc = ops->clr_table(rdev->net->hport, rdev->destid, 1808 rdev->hopcount, table); 1809 1810 module_put(ops->owner); 1811 } 1812 1813 spin_unlock(&rdev->rswitch->lock); 1814 1815 if (lock) 1816 rio_unlock_device(rdev->net->hport, rdev->destid, 1817 rdev->hopcount); 1818 1819 return rc; 1820 } 1821 EXPORT_SYMBOL_GPL(rio_route_clr_table); 1822 1823 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 1824 1825 static bool rio_chan_filter(struct dma_chan *chan, void *arg) 1826 { 1827 struct rio_mport *mport = arg; 1828 1829 /* Check that DMA device belongs to the right MPORT */ 1830 return mport == container_of(chan->device, struct rio_mport, dma); 1831 } 1832 1833 /** 1834 * rio_request_mport_dma - request RapidIO capable DMA channel associated 1835 * with specified local RapidIO mport device. 1836 * @mport: RIO mport to perform DMA data transfers 1837 * 1838 * Returns pointer to allocated DMA channel or NULL if failed. 1839 */ 1840 struct dma_chan *rio_request_mport_dma(struct rio_mport *mport) 1841 { 1842 dma_cap_mask_t mask; 1843 1844 dma_cap_zero(mask); 1845 dma_cap_set(DMA_SLAVE, mask); 1846 return dma_request_channel(mask, rio_chan_filter, mport); 1847 } 1848 EXPORT_SYMBOL_GPL(rio_request_mport_dma); 1849 1850 /** 1851 * rio_request_dma - request RapidIO capable DMA channel that supports 1852 * specified target RapidIO device. 1853 * @rdev: RIO device associated with DMA transfer 1854 * 1855 * Returns pointer to allocated DMA channel or NULL if failed. 1856 */ 1857 struct dma_chan *rio_request_dma(struct rio_dev *rdev) 1858 { 1859 return rio_request_mport_dma(rdev->net->hport); 1860 } 1861 EXPORT_SYMBOL_GPL(rio_request_dma); 1862 1863 /** 1864 * rio_release_dma - release specified DMA channel 1865 * @dchan: DMA channel to release 1866 */ 1867 void rio_release_dma(struct dma_chan *dchan) 1868 { 1869 dma_release_channel(dchan); 1870 } 1871 EXPORT_SYMBOL_GPL(rio_release_dma); 1872 1873 /** 1874 * rio_dma_prep_xfer - RapidIO specific wrapper 1875 * for device_prep_slave_sg callback defined by DMAENGINE. 1876 * @dchan: DMA channel to configure 1877 * @destid: target RapidIO device destination ID 1878 * @data: RIO specific data descriptor 1879 * @direction: DMA data transfer direction (TO or FROM the device) 1880 * @flags: dmaengine defined flags 1881 * 1882 * Initializes RapidIO capable DMA channel for the specified data transfer. 1883 * Uses DMA channel private extension to pass information related to remote 1884 * target RIO device. 1885 * 1886 * Returns: pointer to DMA transaction descriptor if successful, 1887 * error-valued pointer or NULL if failed. 1888 */ 1889 struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan, 1890 u16 destid, struct rio_dma_data *data, 1891 enum dma_transfer_direction direction, unsigned long flags) 1892 { 1893 struct rio_dma_ext rio_ext; 1894 1895 if (dchan->device->device_prep_slave_sg == NULL) { 1896 pr_err("%s: prep_rio_sg == NULL\n", __func__); 1897 return NULL; 1898 } 1899 1900 rio_ext.destid = destid; 1901 rio_ext.rio_addr_u = data->rio_addr_u; 1902 rio_ext.rio_addr = data->rio_addr; 1903 rio_ext.wr_type = data->wr_type; 1904 1905 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len, 1906 direction, flags, &rio_ext); 1907 } 1908 EXPORT_SYMBOL_GPL(rio_dma_prep_xfer); 1909 1910 /** 1911 * rio_dma_prep_slave_sg - RapidIO specific wrapper 1912 * for device_prep_slave_sg callback defined by DMAENGINE. 1913 * @rdev: RIO device control structure 1914 * @dchan: DMA channel to configure 1915 * @data: RIO specific data descriptor 1916 * @direction: DMA data transfer direction (TO or FROM the device) 1917 * @flags: dmaengine defined flags 1918 * 1919 * Initializes RapidIO capable DMA channel for the specified data transfer. 1920 * Uses DMA channel private extension to pass information related to remote 1921 * target RIO device. 1922 * 1923 * Returns: pointer to DMA transaction descriptor if successful, 1924 * error-valued pointer or NULL if failed. 1925 */ 1926 struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev, 1927 struct dma_chan *dchan, struct rio_dma_data *data, 1928 enum dma_transfer_direction direction, unsigned long flags) 1929 { 1930 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags); 1931 } 1932 EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg); 1933 1934 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */ 1935 1936 /** 1937 * rio_find_mport - find RIO mport by its ID 1938 * @mport_id: number (ID) of mport device 1939 * 1940 * Given a RIO mport number, the desired mport is located 1941 * in the global list of mports. If the mport is found, a pointer to its 1942 * data structure is returned. If no mport is found, %NULL is returned. 1943 */ 1944 struct rio_mport *rio_find_mport(int mport_id) 1945 { 1946 struct rio_mport *port; 1947 1948 mutex_lock(&rio_mport_list_lock); 1949 list_for_each_entry(port, &rio_mports, node) { 1950 if (port->id == mport_id) 1951 goto found; 1952 } 1953 port = NULL; 1954 found: 1955 mutex_unlock(&rio_mport_list_lock); 1956 1957 return port; 1958 } 1959 1960 /** 1961 * rio_register_scan - enumeration/discovery method registration interface 1962 * @mport_id: mport device ID for which fabric scan routine has to be set 1963 * (RIO_MPORT_ANY = set for all available mports) 1964 * @scan_ops: enumeration/discovery operations structure 1965 * 1966 * Registers enumeration/discovery operations with RapidIO subsystem and 1967 * attaches it to the specified mport device (or all available mports 1968 * if RIO_MPORT_ANY is specified). 1969 * 1970 * Returns error if the mport already has an enumerator attached to it. 1971 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error). 1972 */ 1973 int rio_register_scan(int mport_id, struct rio_scan *scan_ops) 1974 { 1975 struct rio_mport *port; 1976 struct rio_scan_node *scan; 1977 int rc = 0; 1978 1979 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id); 1980 1981 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) || 1982 !scan_ops) 1983 return -EINVAL; 1984 1985 mutex_lock(&rio_mport_list_lock); 1986 1987 /* 1988 * Check if there is another enumerator already registered for 1989 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators 1990 * for the same mport ID are not supported. 1991 */ 1992 list_for_each_entry(scan, &rio_scans, node) { 1993 if (scan->mport_id == mport_id) { 1994 rc = -EBUSY; 1995 goto err_out; 1996 } 1997 } 1998 1999 /* 2000 * Allocate and initialize new scan registration node. 2001 */ 2002 scan = kzalloc(sizeof(*scan), GFP_KERNEL); 2003 if (!scan) { 2004 rc = -ENOMEM; 2005 goto err_out; 2006 } 2007 2008 scan->mport_id = mport_id; 2009 scan->ops = scan_ops; 2010 2011 /* 2012 * Traverse the list of registered mports to attach this new scan. 2013 * 2014 * The new scan with matching mport ID overrides any previously attached 2015 * scan assuming that old scan (if any) is the default one (based on the 2016 * enumerator registration check above). 2017 * If the new scan is the global one, it will be attached only to mports 2018 * that do not have their own individual operations already attached. 2019 */ 2020 list_for_each_entry(port, &rio_mports, node) { 2021 if (port->id == mport_id) { 2022 port->nscan = scan_ops; 2023 break; 2024 } else if (mport_id == RIO_MPORT_ANY && !port->nscan) 2025 port->nscan = scan_ops; 2026 } 2027 2028 list_add_tail(&scan->node, &rio_scans); 2029 2030 err_out: 2031 mutex_unlock(&rio_mport_list_lock); 2032 2033 return rc; 2034 } 2035 EXPORT_SYMBOL_GPL(rio_register_scan); 2036 2037 /** 2038 * rio_unregister_scan - removes enumeration/discovery method from mport 2039 * @mport_id: mport device ID for which fabric scan routine has to be 2040 * unregistered (RIO_MPORT_ANY = apply to all mports that use 2041 * the specified scan_ops) 2042 * @scan_ops: enumeration/discovery operations structure 2043 * 2044 * Removes enumeration or discovery method assigned to the specified mport 2045 * device. If RIO_MPORT_ANY is specified, removes the specified operations from 2046 * all mports that have them attached. 2047 */ 2048 int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops) 2049 { 2050 struct rio_mport *port; 2051 struct rio_scan_node *scan; 2052 2053 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id); 2054 2055 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) 2056 return -EINVAL; 2057 2058 mutex_lock(&rio_mport_list_lock); 2059 2060 list_for_each_entry(port, &rio_mports, node) 2061 if (port->id == mport_id || 2062 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops)) 2063 port->nscan = NULL; 2064 2065 list_for_each_entry(scan, &rio_scans, node) { 2066 if (scan->mport_id == mport_id) { 2067 list_del(&scan->node); 2068 kfree(scan); 2069 break; 2070 } 2071 } 2072 2073 mutex_unlock(&rio_mport_list_lock); 2074 2075 return 0; 2076 } 2077 EXPORT_SYMBOL_GPL(rio_unregister_scan); 2078 2079 /** 2080 * rio_mport_scan - execute enumeration/discovery on the specified mport 2081 * @mport_id: number (ID) of mport device 2082 */ 2083 int rio_mport_scan(int mport_id) 2084 { 2085 struct rio_mport *port = NULL; 2086 int rc; 2087 2088 mutex_lock(&rio_mport_list_lock); 2089 list_for_each_entry(port, &rio_mports, node) { 2090 if (port->id == mport_id) 2091 goto found; 2092 } 2093 mutex_unlock(&rio_mport_list_lock); 2094 return -ENODEV; 2095 found: 2096 if (!port->nscan) { 2097 mutex_unlock(&rio_mport_list_lock); 2098 return -EINVAL; 2099 } 2100 2101 if (!try_module_get(port->nscan->owner)) { 2102 mutex_unlock(&rio_mport_list_lock); 2103 return -ENODEV; 2104 } 2105 2106 mutex_unlock(&rio_mport_list_lock); 2107 2108 if (port->host_deviceid >= 0) 2109 rc = port->nscan->enumerate(port, 0); 2110 else 2111 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT); 2112 2113 module_put(port->nscan->owner); 2114 return rc; 2115 } 2116 2117 static void rio_fixup_device(struct rio_dev *dev) 2118 { 2119 } 2120 2121 static int rio_init(void) 2122 { 2123 struct rio_dev *dev = NULL; 2124 2125 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) { 2126 rio_fixup_device(dev); 2127 } 2128 return 0; 2129 } 2130 2131 static struct workqueue_struct *rio_wq; 2132 2133 struct rio_disc_work { 2134 struct work_struct work; 2135 struct rio_mport *mport; 2136 }; 2137 2138 static void disc_work_handler(struct work_struct *_work) 2139 { 2140 struct rio_disc_work *work; 2141 2142 work = container_of(_work, struct rio_disc_work, work); 2143 pr_debug("RIO: discovery work for mport %d %s\n", 2144 work->mport->id, work->mport->name); 2145 if (try_module_get(work->mport->nscan->owner)) { 2146 work->mport->nscan->discover(work->mport, 0); 2147 module_put(work->mport->nscan->owner); 2148 } 2149 } 2150 2151 int rio_init_mports(void) 2152 { 2153 struct rio_mport *port; 2154 struct rio_disc_work *work; 2155 int n = 0; 2156 2157 if (!next_portid) 2158 return -ENODEV; 2159 2160 /* 2161 * First, run enumerations and check if we need to perform discovery 2162 * on any of the registered mports. 2163 */ 2164 mutex_lock(&rio_mport_list_lock); 2165 list_for_each_entry(port, &rio_mports, node) { 2166 if (port->host_deviceid >= 0) { 2167 if (port->nscan && try_module_get(port->nscan->owner)) { 2168 port->nscan->enumerate(port, 0); 2169 module_put(port->nscan->owner); 2170 } 2171 } else 2172 n++; 2173 } 2174 mutex_unlock(&rio_mport_list_lock); 2175 2176 if (!n) 2177 goto no_disc; 2178 2179 /* 2180 * If we have mports that require discovery schedule a discovery work 2181 * for each of them. If the code below fails to allocate needed 2182 * resources, exit without error to keep results of enumeration 2183 * process (if any). 2184 * TODO: Implement restart of discovery process for all or 2185 * individual discovering mports. 2186 */ 2187 rio_wq = alloc_workqueue("riodisc", 0, 0); 2188 if (!rio_wq) { 2189 pr_err("RIO: unable allocate rio_wq\n"); 2190 goto no_disc; 2191 } 2192 2193 work = kcalloc(n, sizeof *work, GFP_KERNEL); 2194 if (!work) { 2195 pr_err("RIO: no memory for work struct\n"); 2196 destroy_workqueue(rio_wq); 2197 goto no_disc; 2198 } 2199 2200 n = 0; 2201 mutex_lock(&rio_mport_list_lock); 2202 list_for_each_entry(port, &rio_mports, node) { 2203 if (port->host_deviceid < 0 && port->nscan) { 2204 work[n].mport = port; 2205 INIT_WORK(&work[n].work, disc_work_handler); 2206 queue_work(rio_wq, &work[n].work); 2207 n++; 2208 } 2209 } 2210 2211 flush_workqueue(rio_wq); 2212 mutex_unlock(&rio_mport_list_lock); 2213 pr_debug("RIO: destroy discovery workqueue\n"); 2214 destroy_workqueue(rio_wq); 2215 kfree(work); 2216 2217 no_disc: 2218 rio_init(); 2219 2220 return 0; 2221 } 2222 2223 static int rio_get_hdid(int index) 2224 { 2225 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS) 2226 return -1; 2227 2228 return hdid[index]; 2229 } 2230 2231 int rio_mport_initialize(struct rio_mport *mport) 2232 { 2233 if (next_portid >= RIO_MAX_MPORTS) { 2234 pr_err("RIO: reached specified max number of mports\n"); 2235 return -ENODEV; 2236 } 2237 2238 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING); 2239 mport->id = next_portid++; 2240 mport->host_deviceid = rio_get_hdid(mport->id); 2241 mport->nscan = NULL; 2242 mutex_init(&mport->lock); 2243 mport->pwe_refcnt = 0; 2244 INIT_LIST_HEAD(&mport->pwrites); 2245 2246 return 0; 2247 } 2248 EXPORT_SYMBOL_GPL(rio_mport_initialize); 2249 2250 int rio_register_mport(struct rio_mport *port) 2251 { 2252 struct rio_scan_node *scan = NULL; 2253 int res = 0; 2254 2255 mutex_lock(&rio_mport_list_lock); 2256 2257 /* 2258 * Check if there are any registered enumeration/discovery operations 2259 * that have to be attached to the added mport. 2260 */ 2261 list_for_each_entry(scan, &rio_scans, node) { 2262 if (port->id == scan->mport_id || 2263 scan->mport_id == RIO_MPORT_ANY) { 2264 port->nscan = scan->ops; 2265 if (port->id == scan->mport_id) 2266 break; 2267 } 2268 } 2269 2270 list_add_tail(&port->node, &rio_mports); 2271 mutex_unlock(&rio_mport_list_lock); 2272 2273 dev_set_name(&port->dev, "rapidio%d", port->id); 2274 port->dev.class = &rio_mport_class; 2275 atomic_set(&port->state, RIO_DEVICE_RUNNING); 2276 2277 res = device_register(&port->dev); 2278 if (res) 2279 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n", 2280 port->id, res); 2281 else 2282 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id); 2283 2284 return res; 2285 } 2286 EXPORT_SYMBOL_GPL(rio_register_mport); 2287 2288 static int rio_mport_cleanup_callback(struct device *dev, void *data) 2289 { 2290 struct rio_dev *rdev = to_rio_dev(dev); 2291 2292 if (dev->bus == &rio_bus_type) 2293 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN); 2294 return 0; 2295 } 2296 2297 static int rio_net_remove_children(struct rio_net *net) 2298 { 2299 /* 2300 * Unregister all RapidIO devices residing on this net (this will 2301 * invoke notification of registered subsystem interfaces as well). 2302 */ 2303 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback); 2304 return 0; 2305 } 2306 2307 int rio_unregister_mport(struct rio_mport *port) 2308 { 2309 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id); 2310 2311 /* Transition mport to the SHUTDOWN state */ 2312 if (atomic_cmpxchg(&port->state, 2313 RIO_DEVICE_RUNNING, 2314 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) { 2315 pr_err("RIO: %s unexpected state transition for mport %s\n", 2316 __func__, port->name); 2317 } 2318 2319 if (port->net && port->net->hport == port) { 2320 rio_net_remove_children(port->net); 2321 rio_free_net(port->net); 2322 } 2323 2324 /* 2325 * Unregister all RapidIO devices attached to this mport (this will 2326 * invoke notification of registered subsystem interfaces as well). 2327 */ 2328 mutex_lock(&rio_mport_list_lock); 2329 list_del(&port->node); 2330 mutex_unlock(&rio_mport_list_lock); 2331 device_unregister(&port->dev); 2332 2333 return 0; 2334 } 2335 EXPORT_SYMBOL_GPL(rio_unregister_mport); 2336 2337 EXPORT_SYMBOL_GPL(rio_local_get_device_id); 2338 EXPORT_SYMBOL_GPL(rio_get_device); 2339 EXPORT_SYMBOL_GPL(rio_get_asm); 2340 EXPORT_SYMBOL_GPL(rio_request_inb_dbell); 2341 EXPORT_SYMBOL_GPL(rio_release_inb_dbell); 2342 EXPORT_SYMBOL_GPL(rio_request_outb_dbell); 2343 EXPORT_SYMBOL_GPL(rio_release_outb_dbell); 2344 EXPORT_SYMBOL_GPL(rio_request_inb_mbox); 2345 EXPORT_SYMBOL_GPL(rio_release_inb_mbox); 2346 EXPORT_SYMBOL_GPL(rio_request_outb_mbox); 2347 EXPORT_SYMBOL_GPL(rio_release_outb_mbox); 2348 EXPORT_SYMBOL_GPL(rio_init_mports); 2349