Linux Audio

Check our new training course

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