1 /* 2 * RapidIO mport character device 3 * 4 * Copyright 2014-2015 Integrated Device Technology, Inc. 5 * Alexandre Bounine <alexandre.bounine@idt.com> 6 * Copyright 2014-2015 Prodrive Technologies 7 * Andre van Herk <andre.van.herk@prodrive-technologies.com> 8 * Jerry Jacobs <jerry.jacobs@prodrive-technologies.com> 9 * Copyright (C) 2014 Texas Instruments Incorporated 10 * Aurelien Jacquiot <a-jacquiot@ti.com> 11 * 12 * This program is free software; you can redistribute it and/or modify it 13 * under the terms of the GNU General Public License as published by the 14 * Free Software Foundation; either version 2 of the License, or (at your 15 * option) any later version. 16 */ 17 #include <linux/module.h> 18 #include <linux/kernel.h> 19 #include <linux/cdev.h> 20 #include <linux/ioctl.h> 21 #include <linux/uaccess.h> 22 #include <linux/list.h> 23 #include <linux/fs.h> 24 #include <linux/err.h> 25 #include <linux/net.h> 26 #include <linux/poll.h> 27 #include <linux/spinlock.h> 28 #include <linux/sched.h> 29 #include <linux/kfifo.h> 30 31 #include <linux/mm.h> 32 #include <linux/slab.h> 33 #include <linux/vmalloc.h> 34 #include <linux/mman.h> 35 36 #include <linux/dma-mapping.h> 37 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 38 #include <linux/dmaengine.h> 39 #endif 40 41 #include <linux/rio.h> 42 #include <linux/rio_ids.h> 43 #include <linux/rio_drv.h> 44 #include <linux/rio_mport_cdev.h> 45 46 #include "../rio.h" 47 48 #define DRV_NAME "rio_mport" 49 #define DRV_PREFIX DRV_NAME ": " 50 #define DEV_NAME "rio_mport" 51 #define DRV_VERSION "1.0.0" 52 53 /* Debug output filtering masks */ 54 enum { 55 DBG_NONE = 0, 56 DBG_INIT = BIT(0), /* driver init */ 57 DBG_EXIT = BIT(1), /* driver exit */ 58 DBG_MPORT = BIT(2), /* mport add/remove */ 59 DBG_RDEV = BIT(3), /* RapidIO device add/remove */ 60 DBG_DMA = BIT(4), /* DMA transfer messages */ 61 DBG_MMAP = BIT(5), /* mapping messages */ 62 DBG_IBW = BIT(6), /* inbound window */ 63 DBG_EVENT = BIT(7), /* event handling messages */ 64 DBG_OBW = BIT(8), /* outbound window messages */ 65 DBG_DBELL = BIT(9), /* doorbell messages */ 66 DBG_ALL = ~0, 67 }; 68 69 #ifdef DEBUG 70 #define rmcd_debug(level, fmt, arg...) \ 71 do { \ 72 if (DBG_##level & dbg_level) \ 73 pr_debug(DRV_PREFIX "%s: " fmt "\n", __func__, ##arg); \ 74 } while (0) 75 #else 76 #define rmcd_debug(level, fmt, arg...) \ 77 no_printk(KERN_DEBUG pr_fmt(DRV_PREFIX fmt "\n"), ##arg) 78 #endif 79 80 #define rmcd_warn(fmt, arg...) \ 81 pr_warn(DRV_PREFIX "%s WARNING " fmt "\n", __func__, ##arg) 82 83 #define rmcd_error(fmt, arg...) \ 84 pr_err(DRV_PREFIX "%s ERROR " fmt "\n", __func__, ##arg) 85 86 MODULE_AUTHOR("Jerry Jacobs <jerry.jacobs@prodrive-technologies.com>"); 87 MODULE_AUTHOR("Aurelien Jacquiot <a-jacquiot@ti.com>"); 88 MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>"); 89 MODULE_AUTHOR("Andre van Herk <andre.van.herk@prodrive-technologies.com>"); 90 MODULE_DESCRIPTION("RapidIO mport character device driver"); 91 MODULE_LICENSE("GPL"); 92 MODULE_VERSION(DRV_VERSION); 93 94 static int dma_timeout = 3000; /* DMA transfer timeout in msec */ 95 module_param(dma_timeout, int, S_IRUGO); 96 MODULE_PARM_DESC(dma_timeout, "DMA Transfer Timeout in msec (default: 3000)"); 97 98 #ifdef DEBUG 99 static u32 dbg_level = DBG_NONE; 100 module_param(dbg_level, uint, S_IWUSR | S_IWGRP | S_IRUGO); 101 MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)"); 102 #endif 103 104 /* 105 * An internal DMA coherent buffer 106 */ 107 struct mport_dma_buf { 108 void *ib_base; 109 dma_addr_t ib_phys; 110 u32 ib_size; 111 u64 ib_rio_base; 112 bool ib_map; 113 struct file *filp; 114 }; 115 116 /* 117 * Internal memory mapping structure 118 */ 119 enum rio_mport_map_dir { 120 MAP_INBOUND, 121 MAP_OUTBOUND, 122 MAP_DMA, 123 }; 124 125 struct rio_mport_mapping { 126 struct list_head node; 127 struct mport_dev *md; 128 enum rio_mport_map_dir dir; 129 u16 rioid; 130 u64 rio_addr; 131 dma_addr_t phys_addr; /* for mmap */ 132 void *virt_addr; /* kernel address, for dma_free_coherent */ 133 u64 size; 134 struct kref ref; /* refcount of vmas sharing the mapping */ 135 struct file *filp; 136 }; 137 138 struct rio_mport_dma_map { 139 int valid; 140 u64 length; 141 void *vaddr; 142 dma_addr_t paddr; 143 }; 144 145 #define MPORT_MAX_DMA_BUFS 16 146 #define MPORT_EVENT_DEPTH 10 147 148 /* 149 * mport_dev driver-specific structure that represents mport device 150 * @active mport device status flag 151 * @node list node to maintain list of registered mports 152 * @cdev character device 153 * @dev associated device object 154 * @mport associated subsystem's master port device object 155 * @buf_mutex lock for buffer handling 156 * @file_mutex - lock for open files list 157 * @file_list - list of open files on given mport 158 * @properties properties of this mport 159 * @portwrites queue of inbound portwrites 160 * @pw_lock lock for port write queue 161 * @mappings queue for memory mappings 162 * @dma_chan DMA channels associated with this device 163 * @dma_ref: 164 * @comp: 165 */ 166 struct mport_dev { 167 atomic_t active; 168 struct list_head node; 169 struct cdev cdev; 170 struct device dev; 171 struct rio_mport *mport; 172 struct mutex buf_mutex; 173 struct mutex file_mutex; 174 struct list_head file_list; 175 struct rio_mport_properties properties; 176 struct list_head doorbells; 177 spinlock_t db_lock; 178 struct list_head portwrites; 179 spinlock_t pw_lock; 180 struct list_head mappings; 181 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 182 struct dma_chan *dma_chan; 183 struct kref dma_ref; 184 struct completion comp; 185 #endif 186 }; 187 188 /* 189 * mport_cdev_priv - data structure specific to individual file object 190 * associated with an open device 191 * @md master port character device object 192 * @async_queue - asynchronous notification queue 193 * @list - file objects tracking list 194 * @db_filters inbound doorbell filters for this descriptor 195 * @pw_filters portwrite filters for this descriptor 196 * @event_fifo event fifo for this descriptor 197 * @event_rx_wait wait queue for this descriptor 198 * @fifo_lock lock for event_fifo 199 * @event_mask event mask for this descriptor 200 * @dmach DMA engine channel allocated for specific file object 201 */ 202 struct mport_cdev_priv { 203 struct mport_dev *md; 204 struct fasync_struct *async_queue; 205 struct list_head list; 206 struct list_head db_filters; 207 struct list_head pw_filters; 208 struct kfifo event_fifo; 209 wait_queue_head_t event_rx_wait; 210 spinlock_t fifo_lock; 211 u32 event_mask; /* RIO_DOORBELL, RIO_PORTWRITE */ 212 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 213 struct dma_chan *dmach; 214 struct list_head async_list; 215 spinlock_t req_lock; 216 struct mutex dma_lock; 217 struct kref dma_ref; 218 struct completion comp; 219 #endif 220 }; 221 222 /* 223 * rio_mport_pw_filter - structure to describe a portwrite filter 224 * md_node node in mport device's list 225 * priv_node node in private file object's list 226 * priv reference to private data 227 * filter actual portwrite filter 228 */ 229 struct rio_mport_pw_filter { 230 struct list_head md_node; 231 struct list_head priv_node; 232 struct mport_cdev_priv *priv; 233 struct rio_pw_filter filter; 234 }; 235 236 /* 237 * rio_mport_db_filter - structure to describe a doorbell filter 238 * @data_node reference to device node 239 * @priv_node node in private data 240 * @priv reference to private data 241 * @filter actual doorbell filter 242 */ 243 struct rio_mport_db_filter { 244 struct list_head data_node; 245 struct list_head priv_node; 246 struct mport_cdev_priv *priv; 247 struct rio_doorbell_filter filter; 248 }; 249 250 static LIST_HEAD(mport_devs); 251 static DEFINE_MUTEX(mport_devs_lock); 252 253 #if (0) /* used by commented out portion of poll function : FIXME */ 254 static DECLARE_WAIT_QUEUE_HEAD(mport_cdev_wait); 255 #endif 256 257 static struct class *dev_class; 258 static dev_t dev_number; 259 260 static void mport_release_mapping(struct kref *ref); 261 262 static int rio_mport_maint_rd(struct mport_cdev_priv *priv, void __user *arg, 263 int local) 264 { 265 struct rio_mport *mport = priv->md->mport; 266 struct rio_mport_maint_io maint_io; 267 u32 *buffer; 268 u32 offset; 269 size_t length; 270 int ret, i; 271 272 if (unlikely(copy_from_user(&maint_io, arg, sizeof(maint_io)))) 273 return -EFAULT; 274 275 if ((maint_io.offset % 4) || 276 (maint_io.length == 0) || (maint_io.length % 4) || 277 (maint_io.length + maint_io.offset) > RIO_MAINT_SPACE_SZ) 278 return -EINVAL; 279 280 buffer = vmalloc(maint_io.length); 281 if (buffer == NULL) 282 return -ENOMEM; 283 length = maint_io.length/sizeof(u32); 284 offset = maint_io.offset; 285 286 for (i = 0; i < length; i++) { 287 if (local) 288 ret = __rio_local_read_config_32(mport, 289 offset, &buffer[i]); 290 else 291 ret = rio_mport_read_config_32(mport, maint_io.rioid, 292 maint_io.hopcount, offset, &buffer[i]); 293 if (ret) 294 goto out; 295 296 offset += 4; 297 } 298 299 if (unlikely(copy_to_user((void __user *)(uintptr_t)maint_io.buffer, 300 buffer, maint_io.length))) 301 ret = -EFAULT; 302 out: 303 vfree(buffer); 304 return ret; 305 } 306 307 static int rio_mport_maint_wr(struct mport_cdev_priv *priv, void __user *arg, 308 int local) 309 { 310 struct rio_mport *mport = priv->md->mport; 311 struct rio_mport_maint_io maint_io; 312 u32 *buffer; 313 u32 offset; 314 size_t length; 315 int ret = -EINVAL, i; 316 317 if (unlikely(copy_from_user(&maint_io, arg, sizeof(maint_io)))) 318 return -EFAULT; 319 320 if ((maint_io.offset % 4) || 321 (maint_io.length == 0) || (maint_io.length % 4) || 322 (maint_io.length + maint_io.offset) > RIO_MAINT_SPACE_SZ) 323 return -EINVAL; 324 325 buffer = vmalloc(maint_io.length); 326 if (buffer == NULL) 327 return -ENOMEM; 328 length = maint_io.length; 329 330 if (unlikely(copy_from_user(buffer, 331 (void __user *)(uintptr_t)maint_io.buffer, length))) { 332 ret = -EFAULT; 333 goto out; 334 } 335 336 offset = maint_io.offset; 337 length /= sizeof(u32); 338 339 for (i = 0; i < length; i++) { 340 if (local) 341 ret = __rio_local_write_config_32(mport, 342 offset, buffer[i]); 343 else 344 ret = rio_mport_write_config_32(mport, maint_io.rioid, 345 maint_io.hopcount, 346 offset, buffer[i]); 347 if (ret) 348 goto out; 349 350 offset += 4; 351 } 352 353 out: 354 vfree(buffer); 355 return ret; 356 } 357 358 359 /* 360 * Inbound/outbound memory mapping functions 361 */ 362 static int 363 rio_mport_create_outbound_mapping(struct mport_dev *md, struct file *filp, 364 u16 rioid, u64 raddr, u32 size, 365 dma_addr_t *paddr) 366 { 367 struct rio_mport *mport = md->mport; 368 struct rio_mport_mapping *map; 369 int ret; 370 371 rmcd_debug(OBW, "did=%d ra=0x%llx sz=0x%x", rioid, raddr, size); 372 373 map = kzalloc(sizeof(*map), GFP_KERNEL); 374 if (map == NULL) 375 return -ENOMEM; 376 377 ret = rio_map_outb_region(mport, rioid, raddr, size, 0, paddr); 378 if (ret < 0) 379 goto err_map_outb; 380 381 map->dir = MAP_OUTBOUND; 382 map->rioid = rioid; 383 map->rio_addr = raddr; 384 map->size = size; 385 map->phys_addr = *paddr; 386 map->filp = filp; 387 map->md = md; 388 kref_init(&map->ref); 389 list_add_tail(&map->node, &md->mappings); 390 return 0; 391 err_map_outb: 392 kfree(map); 393 return ret; 394 } 395 396 static int 397 rio_mport_get_outbound_mapping(struct mport_dev *md, struct file *filp, 398 u16 rioid, u64 raddr, u32 size, 399 dma_addr_t *paddr) 400 { 401 struct rio_mport_mapping *map; 402 int err = -ENOMEM; 403 404 mutex_lock(&md->buf_mutex); 405 list_for_each_entry(map, &md->mappings, node) { 406 if (map->dir != MAP_OUTBOUND) 407 continue; 408 if (rioid == map->rioid && 409 raddr == map->rio_addr && size == map->size) { 410 *paddr = map->phys_addr; 411 err = 0; 412 break; 413 } else if (rioid == map->rioid && 414 raddr < (map->rio_addr + map->size - 1) && 415 (raddr + size) > map->rio_addr) { 416 err = -EBUSY; 417 break; 418 } 419 } 420 421 /* If not found, create new */ 422 if (err == -ENOMEM) 423 err = rio_mport_create_outbound_mapping(md, filp, rioid, raddr, 424 size, paddr); 425 mutex_unlock(&md->buf_mutex); 426 return err; 427 } 428 429 static int rio_mport_obw_map(struct file *filp, void __user *arg) 430 { 431 struct mport_cdev_priv *priv = filp->private_data; 432 struct mport_dev *data = priv->md; 433 struct rio_mmap map; 434 dma_addr_t paddr; 435 int ret; 436 437 if (unlikely(copy_from_user(&map, arg, sizeof(map)))) 438 return -EFAULT; 439 440 rmcd_debug(OBW, "did=%d ra=0x%llx sz=0x%llx", 441 map.rioid, map.rio_addr, map.length); 442 443 ret = rio_mport_get_outbound_mapping(data, filp, map.rioid, 444 map.rio_addr, map.length, &paddr); 445 if (ret < 0) { 446 rmcd_error("Failed to set OBW err= %d", ret); 447 return ret; 448 } 449 450 map.handle = paddr; 451 452 if (unlikely(copy_to_user(arg, &map, sizeof(map)))) 453 return -EFAULT; 454 return 0; 455 } 456 457 /* 458 * rio_mport_obw_free() - unmap an OutBound Window from RapidIO address space 459 * 460 * @priv: driver private data 461 * @arg: buffer handle returned by allocation routine 462 */ 463 static int rio_mport_obw_free(struct file *filp, void __user *arg) 464 { 465 struct mport_cdev_priv *priv = filp->private_data; 466 struct mport_dev *md = priv->md; 467 u64 handle; 468 struct rio_mport_mapping *map, *_map; 469 470 if (!md->mport->ops->unmap_outb) 471 return -EPROTONOSUPPORT; 472 473 if (copy_from_user(&handle, arg, sizeof(handle))) 474 return -EFAULT; 475 476 rmcd_debug(OBW, "h=0x%llx", handle); 477 478 mutex_lock(&md->buf_mutex); 479 list_for_each_entry_safe(map, _map, &md->mappings, node) { 480 if (map->dir == MAP_OUTBOUND && map->phys_addr == handle) { 481 if (map->filp == filp) { 482 rmcd_debug(OBW, "kref_put h=0x%llx", handle); 483 map->filp = NULL; 484 kref_put(&map->ref, mport_release_mapping); 485 } 486 break; 487 } 488 } 489 mutex_unlock(&md->buf_mutex); 490 491 return 0; 492 } 493 494 /* 495 * maint_hdid_set() - Set the host Device ID 496 * @priv: driver private data 497 * @arg: Device Id 498 */ 499 static int maint_hdid_set(struct mport_cdev_priv *priv, void __user *arg) 500 { 501 struct mport_dev *md = priv->md; 502 u16 hdid; 503 504 if (copy_from_user(&hdid, arg, sizeof(hdid))) 505 return -EFAULT; 506 507 md->mport->host_deviceid = hdid; 508 md->properties.hdid = hdid; 509 rio_local_set_device_id(md->mport, hdid); 510 511 rmcd_debug(MPORT, "Set host device Id to %d", hdid); 512 513 return 0; 514 } 515 516 /* 517 * maint_comptag_set() - Set the host Component Tag 518 * @priv: driver private data 519 * @arg: Component Tag 520 */ 521 static int maint_comptag_set(struct mport_cdev_priv *priv, void __user *arg) 522 { 523 struct mport_dev *md = priv->md; 524 u32 comptag; 525 526 if (copy_from_user(&comptag, arg, sizeof(comptag))) 527 return -EFAULT; 528 529 rio_local_write_config_32(md->mport, RIO_COMPONENT_TAG_CSR, comptag); 530 531 rmcd_debug(MPORT, "Set host Component Tag to %d", comptag); 532 533 return 0; 534 } 535 536 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 537 538 struct mport_dma_req { 539 struct kref refcount; 540 struct list_head node; 541 struct file *filp; 542 struct mport_cdev_priv *priv; 543 enum rio_transfer_sync sync; 544 struct sg_table sgt; 545 struct page **page_list; 546 unsigned int nr_pages; 547 struct rio_mport_mapping *map; 548 struct dma_chan *dmach; 549 enum dma_data_direction dir; 550 dma_cookie_t cookie; 551 enum dma_status status; 552 struct completion req_comp; 553 }; 554 555 static void mport_release_def_dma(struct kref *dma_ref) 556 { 557 struct mport_dev *md = 558 container_of(dma_ref, struct mport_dev, dma_ref); 559 560 rmcd_debug(EXIT, "DMA_%d", md->dma_chan->chan_id); 561 rio_release_dma(md->dma_chan); 562 md->dma_chan = NULL; 563 } 564 565 static void mport_release_dma(struct kref *dma_ref) 566 { 567 struct mport_cdev_priv *priv = 568 container_of(dma_ref, struct mport_cdev_priv, dma_ref); 569 570 rmcd_debug(EXIT, "DMA_%d", priv->dmach->chan_id); 571 complete(&priv->comp); 572 } 573 574 static void dma_req_free(struct kref *ref) 575 { 576 struct mport_dma_req *req = container_of(ref, struct mport_dma_req, 577 refcount); 578 struct mport_cdev_priv *priv = req->priv; 579 unsigned int i; 580 581 dma_unmap_sg(req->dmach->device->dev, 582 req->sgt.sgl, req->sgt.nents, req->dir); 583 sg_free_table(&req->sgt); 584 if (req->page_list) { 585 for (i = 0; i < req->nr_pages; i++) 586 put_page(req->page_list[i]); 587 kfree(req->page_list); 588 } 589 590 if (req->map) { 591 mutex_lock(&req->map->md->buf_mutex); 592 kref_put(&req->map->ref, mport_release_mapping); 593 mutex_unlock(&req->map->md->buf_mutex); 594 } 595 596 kref_put(&priv->dma_ref, mport_release_dma); 597 598 kfree(req); 599 } 600 601 static void dma_xfer_callback(void *param) 602 { 603 struct mport_dma_req *req = (struct mport_dma_req *)param; 604 struct mport_cdev_priv *priv = req->priv; 605 606 req->status = dma_async_is_tx_complete(priv->dmach, req->cookie, 607 NULL, NULL); 608 complete(&req->req_comp); 609 kref_put(&req->refcount, dma_req_free); 610 } 611 612 /* 613 * prep_dma_xfer() - Configure and send request to DMAengine to prepare DMA 614 * transfer object. 615 * Returns pointer to DMA transaction descriptor allocated by DMA driver on 616 * success or ERR_PTR (and/or NULL) if failed. Caller must check returned 617 * non-NULL pointer using IS_ERR macro. 618 */ 619 static struct dma_async_tx_descriptor 620 *prep_dma_xfer(struct dma_chan *chan, struct rio_transfer_io *transfer, 621 struct sg_table *sgt, int nents, enum dma_transfer_direction dir, 622 enum dma_ctrl_flags flags) 623 { 624 struct rio_dma_data tx_data; 625 626 tx_data.sg = sgt->sgl; 627 tx_data.sg_len = nents; 628 tx_data.rio_addr_u = 0; 629 tx_data.rio_addr = transfer->rio_addr; 630 if (dir == DMA_MEM_TO_DEV) { 631 switch (transfer->method) { 632 case RIO_EXCHANGE_NWRITE: 633 tx_data.wr_type = RDW_ALL_NWRITE; 634 break; 635 case RIO_EXCHANGE_NWRITE_R_ALL: 636 tx_data.wr_type = RDW_ALL_NWRITE_R; 637 break; 638 case RIO_EXCHANGE_NWRITE_R: 639 tx_data.wr_type = RDW_LAST_NWRITE_R; 640 break; 641 case RIO_EXCHANGE_DEFAULT: 642 tx_data.wr_type = RDW_DEFAULT; 643 break; 644 default: 645 return ERR_PTR(-EINVAL); 646 } 647 } 648 649 return rio_dma_prep_xfer(chan, transfer->rioid, &tx_data, dir, flags); 650 } 651 652 /* Request DMA channel associated with this mport device. 653 * Try to request DMA channel for every new process that opened given 654 * mport. If a new DMA channel is not available use default channel 655 * which is the first DMA channel opened on mport device. 656 */ 657 static int get_dma_channel(struct mport_cdev_priv *priv) 658 { 659 mutex_lock(&priv->dma_lock); 660 if (!priv->dmach) { 661 priv->dmach = rio_request_mport_dma(priv->md->mport); 662 if (!priv->dmach) { 663 /* Use default DMA channel if available */ 664 if (priv->md->dma_chan) { 665 priv->dmach = priv->md->dma_chan; 666 kref_get(&priv->md->dma_ref); 667 } else { 668 rmcd_error("Failed to get DMA channel"); 669 mutex_unlock(&priv->dma_lock); 670 return -ENODEV; 671 } 672 } else if (!priv->md->dma_chan) { 673 /* Register default DMA channel if we do not have one */ 674 priv->md->dma_chan = priv->dmach; 675 kref_init(&priv->md->dma_ref); 676 rmcd_debug(DMA, "Register DMA_chan %d as default", 677 priv->dmach->chan_id); 678 } 679 680 kref_init(&priv->dma_ref); 681 init_completion(&priv->comp); 682 } 683 684 kref_get(&priv->dma_ref); 685 mutex_unlock(&priv->dma_lock); 686 return 0; 687 } 688 689 static void put_dma_channel(struct mport_cdev_priv *priv) 690 { 691 kref_put(&priv->dma_ref, mport_release_dma); 692 } 693 694 /* 695 * DMA transfer functions 696 */ 697 static int do_dma_request(struct mport_dma_req *req, 698 struct rio_transfer_io *xfer, 699 enum rio_transfer_sync sync, int nents) 700 { 701 struct mport_cdev_priv *priv; 702 struct sg_table *sgt; 703 struct dma_chan *chan; 704 struct dma_async_tx_descriptor *tx; 705 dma_cookie_t cookie; 706 unsigned long tmo = msecs_to_jiffies(dma_timeout); 707 enum dma_transfer_direction dir; 708 long wret; 709 int ret = 0; 710 711 priv = req->priv; 712 sgt = &req->sgt; 713 714 chan = priv->dmach; 715 dir = (req->dir == DMA_FROM_DEVICE) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; 716 717 rmcd_debug(DMA, "%s(%d) uses %s for DMA_%s", 718 current->comm, task_pid_nr(current), 719 dev_name(&chan->dev->device), 720 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE"); 721 722 /* Initialize DMA transaction request */ 723 tx = prep_dma_xfer(chan, xfer, sgt, nents, dir, 724 DMA_CTRL_ACK | DMA_PREP_INTERRUPT); 725 726 if (!tx) { 727 rmcd_debug(DMA, "prep error for %s A:0x%llx L:0x%llx", 728 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", 729 xfer->rio_addr, xfer->length); 730 ret = -EIO; 731 goto err_out; 732 } else if (IS_ERR(tx)) { 733 ret = PTR_ERR(tx); 734 rmcd_debug(DMA, "prep error %d for %s A:0x%llx L:0x%llx", ret, 735 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", 736 xfer->rio_addr, xfer->length); 737 goto err_out; 738 } 739 740 tx->callback = dma_xfer_callback; 741 tx->callback_param = req; 742 743 req->dmach = chan; 744 req->sync = sync; 745 req->status = DMA_IN_PROGRESS; 746 init_completion(&req->req_comp); 747 kref_get(&req->refcount); 748 749 cookie = dmaengine_submit(tx); 750 req->cookie = cookie; 751 752 rmcd_debug(DMA, "pid=%d DMA_%s tx_cookie = %d", task_pid_nr(current), 753 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie); 754 755 if (dma_submit_error(cookie)) { 756 rmcd_error("submit err=%d (addr:0x%llx len:0x%llx)", 757 cookie, xfer->rio_addr, xfer->length); 758 kref_put(&req->refcount, dma_req_free); 759 ret = -EIO; 760 goto err_out; 761 } 762 763 dma_async_issue_pending(chan); 764 765 if (sync == RIO_TRANSFER_ASYNC) { 766 spin_lock(&priv->req_lock); 767 list_add_tail(&req->node, &priv->async_list); 768 spin_unlock(&priv->req_lock); 769 return cookie; 770 } else if (sync == RIO_TRANSFER_FAF) 771 return 0; 772 773 wret = wait_for_completion_interruptible_timeout(&req->req_comp, tmo); 774 775 if (wret == 0) { 776 /* Timeout on wait occurred */ 777 rmcd_error("%s(%d) timed out waiting for DMA_%s %d", 778 current->comm, task_pid_nr(current), 779 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie); 780 return -ETIMEDOUT; 781 } else if (wret == -ERESTARTSYS) { 782 /* Wait_for_completion was interrupted by a signal but DMA may 783 * be in progress 784 */ 785 rmcd_error("%s(%d) wait for DMA_%s %d was interrupted", 786 current->comm, task_pid_nr(current), 787 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie); 788 return -EINTR; 789 } 790 791 if (req->status != DMA_COMPLETE) { 792 /* DMA transaction completion was signaled with error */ 793 rmcd_error("%s(%d) DMA_%s %d completed with status %d (ret=%d)", 794 current->comm, task_pid_nr(current), 795 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", 796 cookie, req->status, ret); 797 ret = -EIO; 798 } 799 800 err_out: 801 return ret; 802 } 803 804 /* 805 * rio_dma_transfer() - Perform RapidIO DMA data transfer to/from 806 * the remote RapidIO device 807 * @filp: file pointer associated with the call 808 * @transfer_mode: DMA transfer mode 809 * @sync: synchronization mode 810 * @dir: DMA transfer direction (DMA_MEM_TO_DEV = write OR 811 * DMA_DEV_TO_MEM = read) 812 * @xfer: data transfer descriptor structure 813 */ 814 static int 815 rio_dma_transfer(struct file *filp, u32 transfer_mode, 816 enum rio_transfer_sync sync, enum dma_data_direction dir, 817 struct rio_transfer_io *xfer) 818 { 819 struct mport_cdev_priv *priv = filp->private_data; 820 unsigned long nr_pages = 0; 821 struct page **page_list = NULL; 822 struct mport_dma_req *req; 823 struct mport_dev *md = priv->md; 824 struct dma_chan *chan; 825 int i, ret; 826 int nents; 827 828 if (xfer->length == 0) 829 return -EINVAL; 830 req = kzalloc(sizeof(*req), GFP_KERNEL); 831 if (!req) 832 return -ENOMEM; 833 834 kref_init(&req->refcount); 835 836 ret = get_dma_channel(priv); 837 if (ret) { 838 kfree(req); 839 return ret; 840 } 841 842 /* 843 * If parameter loc_addr != NULL, we are transferring data from/to 844 * data buffer allocated in user-space: lock in memory user-space 845 * buffer pages and build an SG table for DMA transfer request 846 * 847 * Otherwise (loc_addr == NULL) contiguous kernel-space buffer is 848 * used for DMA data transfers: build single entry SG table using 849 * offset within the internal buffer specified by handle parameter. 850 */ 851 if (xfer->loc_addr) { 852 unsigned int offset; 853 long pinned; 854 855 offset = lower_32_bits(offset_in_page(xfer->loc_addr)); 856 nr_pages = PAGE_ALIGN(xfer->length + offset) >> PAGE_SHIFT; 857 858 page_list = kmalloc_array(nr_pages, 859 sizeof(*page_list), GFP_KERNEL); 860 if (page_list == NULL) { 861 ret = -ENOMEM; 862 goto err_req; 863 } 864 865 pinned = get_user_pages_fast( 866 (unsigned long)xfer->loc_addr & PAGE_MASK, 867 nr_pages, dir == DMA_FROM_DEVICE, page_list); 868 869 if (pinned != nr_pages) { 870 if (pinned < 0) { 871 rmcd_error("get_user_pages_unlocked err=%ld", 872 pinned); 873 nr_pages = 0; 874 } else 875 rmcd_error("pinned %ld out of %ld pages", 876 pinned, nr_pages); 877 ret = -EFAULT; 878 goto err_pg; 879 } 880 881 ret = sg_alloc_table_from_pages(&req->sgt, page_list, nr_pages, 882 offset, xfer->length, GFP_KERNEL); 883 if (ret) { 884 rmcd_error("sg_alloc_table failed with err=%d", ret); 885 goto err_pg; 886 } 887 888 req->page_list = page_list; 889 req->nr_pages = nr_pages; 890 } else { 891 dma_addr_t baddr; 892 struct rio_mport_mapping *map; 893 894 baddr = (dma_addr_t)xfer->handle; 895 896 mutex_lock(&md->buf_mutex); 897 list_for_each_entry(map, &md->mappings, node) { 898 if (baddr >= map->phys_addr && 899 baddr < (map->phys_addr + map->size)) { 900 kref_get(&map->ref); 901 req->map = map; 902 break; 903 } 904 } 905 mutex_unlock(&md->buf_mutex); 906 907 if (req->map == NULL) { 908 ret = -ENOMEM; 909 goto err_req; 910 } 911 912 if (xfer->length + xfer->offset > map->size) { 913 ret = -EINVAL; 914 goto err_req; 915 } 916 917 ret = sg_alloc_table(&req->sgt, 1, GFP_KERNEL); 918 if (unlikely(ret)) { 919 rmcd_error("sg_alloc_table failed for internal buf"); 920 goto err_req; 921 } 922 923 sg_set_buf(req->sgt.sgl, 924 map->virt_addr + (baddr - map->phys_addr) + 925 xfer->offset, xfer->length); 926 } 927 928 req->dir = dir; 929 req->filp = filp; 930 req->priv = priv; 931 chan = priv->dmach; 932 933 nents = dma_map_sg(chan->device->dev, 934 req->sgt.sgl, req->sgt.nents, dir); 935 if (nents == 0) { 936 rmcd_error("Failed to map SG list"); 937 ret = -EFAULT; 938 goto err_pg; 939 } 940 941 ret = do_dma_request(req, xfer, sync, nents); 942 943 if (ret >= 0) { 944 if (sync == RIO_TRANSFER_ASYNC) 945 return ret; /* return ASYNC cookie */ 946 } else { 947 rmcd_debug(DMA, "do_dma_request failed with err=%d", ret); 948 } 949 950 err_pg: 951 if (!req->page_list) { 952 for (i = 0; i < nr_pages; i++) 953 put_page(page_list[i]); 954 kfree(page_list); 955 } 956 err_req: 957 kref_put(&req->refcount, dma_req_free); 958 return ret; 959 } 960 961 static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg) 962 { 963 struct mport_cdev_priv *priv = filp->private_data; 964 struct rio_transaction transaction; 965 struct rio_transfer_io *transfer; 966 enum dma_data_direction dir; 967 int i, ret = 0; 968 969 if (unlikely(copy_from_user(&transaction, arg, sizeof(transaction)))) 970 return -EFAULT; 971 972 if (transaction.count != 1) /* only single transfer for now */ 973 return -EINVAL; 974 975 if ((transaction.transfer_mode & 976 priv->md->properties.transfer_mode) == 0) 977 return -ENODEV; 978 979 transfer = vmalloc(transaction.count * sizeof(*transfer)); 980 if (!transfer) 981 return -ENOMEM; 982 983 if (unlikely(copy_from_user(transfer, 984 (void __user *)(uintptr_t)transaction.block, 985 transaction.count * sizeof(*transfer)))) { 986 ret = -EFAULT; 987 goto out_free; 988 } 989 990 dir = (transaction.dir == RIO_TRANSFER_DIR_READ) ? 991 DMA_FROM_DEVICE : DMA_TO_DEVICE; 992 for (i = 0; i < transaction.count && ret == 0; i++) 993 ret = rio_dma_transfer(filp, transaction.transfer_mode, 994 transaction.sync, dir, &transfer[i]); 995 996 if (unlikely(copy_to_user((void __user *)(uintptr_t)transaction.block, 997 transfer, 998 transaction.count * sizeof(*transfer)))) 999 ret = -EFAULT; 1000 1001 out_free: 1002 vfree(transfer); 1003 1004 return ret; 1005 } 1006 1007 static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg) 1008 { 1009 struct mport_cdev_priv *priv; 1010 struct mport_dev *md; 1011 struct rio_async_tx_wait w_param; 1012 struct mport_dma_req *req; 1013 dma_cookie_t cookie; 1014 unsigned long tmo; 1015 long wret; 1016 int found = 0; 1017 int ret; 1018 1019 priv = (struct mport_cdev_priv *)filp->private_data; 1020 md = priv->md; 1021 1022 if (unlikely(copy_from_user(&w_param, arg, sizeof(w_param)))) 1023 return -EFAULT; 1024 1025 cookie = w_param.token; 1026 if (w_param.timeout) 1027 tmo = msecs_to_jiffies(w_param.timeout); 1028 else /* Use default DMA timeout */ 1029 tmo = msecs_to_jiffies(dma_timeout); 1030 1031 spin_lock(&priv->req_lock); 1032 list_for_each_entry(req, &priv->async_list, node) { 1033 if (req->cookie == cookie) { 1034 list_del(&req->node); 1035 found = 1; 1036 break; 1037 } 1038 } 1039 spin_unlock(&priv->req_lock); 1040 1041 if (!found) 1042 return -EAGAIN; 1043 1044 wret = wait_for_completion_interruptible_timeout(&req->req_comp, tmo); 1045 1046 if (wret == 0) { 1047 /* Timeout on wait occurred */ 1048 rmcd_error("%s(%d) timed out waiting for ASYNC DMA_%s", 1049 current->comm, task_pid_nr(current), 1050 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE"); 1051 ret = -ETIMEDOUT; 1052 goto err_tmo; 1053 } else if (wret == -ERESTARTSYS) { 1054 /* Wait_for_completion was interrupted by a signal but DMA may 1055 * be still in progress 1056 */ 1057 rmcd_error("%s(%d) wait for ASYNC DMA_%s was interrupted", 1058 current->comm, task_pid_nr(current), 1059 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE"); 1060 ret = -EINTR; 1061 goto err_tmo; 1062 } 1063 1064 if (req->status != DMA_COMPLETE) { 1065 /* DMA transaction completion signaled with transfer error */ 1066 rmcd_error("%s(%d) ASYNC DMA_%s completion with status %d", 1067 current->comm, task_pid_nr(current), 1068 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE", 1069 req->status); 1070 ret = -EIO; 1071 } else 1072 ret = 0; 1073 1074 if (req->status != DMA_IN_PROGRESS && req->status != DMA_PAUSED) 1075 kref_put(&req->refcount, dma_req_free); 1076 1077 return ret; 1078 1079 err_tmo: 1080 /* Return request back into async queue */ 1081 spin_lock(&priv->req_lock); 1082 list_add_tail(&req->node, &priv->async_list); 1083 spin_unlock(&priv->req_lock); 1084 return ret; 1085 } 1086 1087 static int rio_mport_create_dma_mapping(struct mport_dev *md, struct file *filp, 1088 u64 size, struct rio_mport_mapping **mapping) 1089 { 1090 struct rio_mport_mapping *map; 1091 1092 map = kzalloc(sizeof(*map), GFP_KERNEL); 1093 if (map == NULL) 1094 return -ENOMEM; 1095 1096 map->virt_addr = dma_alloc_coherent(md->mport->dev.parent, size, 1097 &map->phys_addr, GFP_KERNEL); 1098 if (map->virt_addr == NULL) { 1099 kfree(map); 1100 return -ENOMEM; 1101 } 1102 1103 map->dir = MAP_DMA; 1104 map->size = size; 1105 map->filp = filp; 1106 map->md = md; 1107 kref_init(&map->ref); 1108 mutex_lock(&md->buf_mutex); 1109 list_add_tail(&map->node, &md->mappings); 1110 mutex_unlock(&md->buf_mutex); 1111 *mapping = map; 1112 1113 return 0; 1114 } 1115 1116 static int rio_mport_alloc_dma(struct file *filp, void __user *arg) 1117 { 1118 struct mport_cdev_priv *priv = filp->private_data; 1119 struct mport_dev *md = priv->md; 1120 struct rio_dma_mem map; 1121 struct rio_mport_mapping *mapping = NULL; 1122 int ret; 1123 1124 if (unlikely(copy_from_user(&map, arg, sizeof(map)))) 1125 return -EFAULT; 1126 1127 ret = rio_mport_create_dma_mapping(md, filp, map.length, &mapping); 1128 if (ret) 1129 return ret; 1130 1131 map.dma_handle = mapping->phys_addr; 1132 1133 if (unlikely(copy_to_user(arg, &map, sizeof(map)))) { 1134 mutex_lock(&md->buf_mutex); 1135 kref_put(&mapping->ref, mport_release_mapping); 1136 mutex_unlock(&md->buf_mutex); 1137 return -EFAULT; 1138 } 1139 1140 return 0; 1141 } 1142 1143 static int rio_mport_free_dma(struct file *filp, void __user *arg) 1144 { 1145 struct mport_cdev_priv *priv = filp->private_data; 1146 struct mport_dev *md = priv->md; 1147 u64 handle; 1148 int ret = -EFAULT; 1149 struct rio_mport_mapping *map, *_map; 1150 1151 if (copy_from_user(&handle, arg, sizeof(handle))) 1152 return -EFAULT; 1153 rmcd_debug(EXIT, "filp=%p", filp); 1154 1155 mutex_lock(&md->buf_mutex); 1156 list_for_each_entry_safe(map, _map, &md->mappings, node) { 1157 if (map->dir == MAP_DMA && map->phys_addr == handle && 1158 map->filp == filp) { 1159 kref_put(&map->ref, mport_release_mapping); 1160 ret = 0; 1161 break; 1162 } 1163 } 1164 mutex_unlock(&md->buf_mutex); 1165 1166 if (ret == -EFAULT) { 1167 rmcd_debug(DMA, "ERR no matching mapping"); 1168 return ret; 1169 } 1170 1171 return 0; 1172 } 1173 #else 1174 static int rio_mport_transfer_ioctl(struct file *filp, void *arg) 1175 { 1176 return -ENODEV; 1177 } 1178 1179 static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg) 1180 { 1181 return -ENODEV; 1182 } 1183 1184 static int rio_mport_alloc_dma(struct file *filp, void __user *arg) 1185 { 1186 return -ENODEV; 1187 } 1188 1189 static int rio_mport_free_dma(struct file *filp, void __user *arg) 1190 { 1191 return -ENODEV; 1192 } 1193 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */ 1194 1195 /* 1196 * Inbound/outbound memory mapping functions 1197 */ 1198 1199 static int 1200 rio_mport_create_inbound_mapping(struct mport_dev *md, struct file *filp, 1201 u64 raddr, u64 size, 1202 struct rio_mport_mapping **mapping) 1203 { 1204 struct rio_mport *mport = md->mport; 1205 struct rio_mport_mapping *map; 1206 int ret; 1207 1208 /* rio_map_inb_region() accepts u32 size */ 1209 if (size > 0xffffffff) 1210 return -EINVAL; 1211 1212 map = kzalloc(sizeof(*map), GFP_KERNEL); 1213 if (map == NULL) 1214 return -ENOMEM; 1215 1216 map->virt_addr = dma_alloc_coherent(mport->dev.parent, size, 1217 &map->phys_addr, GFP_KERNEL); 1218 if (map->virt_addr == NULL) { 1219 ret = -ENOMEM; 1220 goto err_dma_alloc; 1221 } 1222 1223 if (raddr == RIO_MAP_ANY_ADDR) 1224 raddr = map->phys_addr; 1225 ret = rio_map_inb_region(mport, map->phys_addr, raddr, (u32)size, 0); 1226 if (ret < 0) 1227 goto err_map_inb; 1228 1229 map->dir = MAP_INBOUND; 1230 map->rio_addr = raddr; 1231 map->size = size; 1232 map->filp = filp; 1233 map->md = md; 1234 kref_init(&map->ref); 1235 mutex_lock(&md->buf_mutex); 1236 list_add_tail(&map->node, &md->mappings); 1237 mutex_unlock(&md->buf_mutex); 1238 *mapping = map; 1239 return 0; 1240 1241 err_map_inb: 1242 dma_free_coherent(mport->dev.parent, size, 1243 map->virt_addr, map->phys_addr); 1244 err_dma_alloc: 1245 kfree(map); 1246 return ret; 1247 } 1248 1249 static int 1250 rio_mport_get_inbound_mapping(struct mport_dev *md, struct file *filp, 1251 u64 raddr, u64 size, 1252 struct rio_mport_mapping **mapping) 1253 { 1254 struct rio_mport_mapping *map; 1255 int err = -ENOMEM; 1256 1257 if (raddr == RIO_MAP_ANY_ADDR) 1258 goto get_new; 1259 1260 mutex_lock(&md->buf_mutex); 1261 list_for_each_entry(map, &md->mappings, node) { 1262 if (map->dir != MAP_INBOUND) 1263 continue; 1264 if (raddr == map->rio_addr && size == map->size) { 1265 /* allow exact match only */ 1266 *mapping = map; 1267 err = 0; 1268 break; 1269 } else if (raddr < (map->rio_addr + map->size - 1) && 1270 (raddr + size) > map->rio_addr) { 1271 err = -EBUSY; 1272 break; 1273 } 1274 } 1275 mutex_unlock(&md->buf_mutex); 1276 1277 if (err != -ENOMEM) 1278 return err; 1279 get_new: 1280 /* not found, create new */ 1281 return rio_mport_create_inbound_mapping(md, filp, raddr, size, mapping); 1282 } 1283 1284 static int rio_mport_map_inbound(struct file *filp, void __user *arg) 1285 { 1286 struct mport_cdev_priv *priv = filp->private_data; 1287 struct mport_dev *md = priv->md; 1288 struct rio_mmap map; 1289 struct rio_mport_mapping *mapping = NULL; 1290 int ret; 1291 1292 if (!md->mport->ops->map_inb) 1293 return -EPROTONOSUPPORT; 1294 if (unlikely(copy_from_user(&map, arg, sizeof(map)))) 1295 return -EFAULT; 1296 1297 rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp); 1298 1299 ret = rio_mport_get_inbound_mapping(md, filp, map.rio_addr, 1300 map.length, &mapping); 1301 if (ret) 1302 return ret; 1303 1304 map.handle = mapping->phys_addr; 1305 map.rio_addr = mapping->rio_addr; 1306 1307 if (unlikely(copy_to_user(arg, &map, sizeof(map)))) { 1308 /* Delete mapping if it was created by this request */ 1309 if (ret == 0 && mapping->filp == filp) { 1310 mutex_lock(&md->buf_mutex); 1311 kref_put(&mapping->ref, mport_release_mapping); 1312 mutex_unlock(&md->buf_mutex); 1313 } 1314 return -EFAULT; 1315 } 1316 1317 return 0; 1318 } 1319 1320 /* 1321 * rio_mport_inbound_free() - unmap from RapidIO address space and free 1322 * previously allocated inbound DMA coherent buffer 1323 * @priv: driver private data 1324 * @arg: buffer handle returned by allocation routine 1325 */ 1326 static int rio_mport_inbound_free(struct file *filp, void __user *arg) 1327 { 1328 struct mport_cdev_priv *priv = filp->private_data; 1329 struct mport_dev *md = priv->md; 1330 u64 handle; 1331 struct rio_mport_mapping *map, *_map; 1332 1333 rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp); 1334 1335 if (!md->mport->ops->unmap_inb) 1336 return -EPROTONOSUPPORT; 1337 1338 if (copy_from_user(&handle, arg, sizeof(handle))) 1339 return -EFAULT; 1340 1341 mutex_lock(&md->buf_mutex); 1342 list_for_each_entry_safe(map, _map, &md->mappings, node) { 1343 if (map->dir == MAP_INBOUND && map->phys_addr == handle) { 1344 if (map->filp == filp) { 1345 map->filp = NULL; 1346 kref_put(&map->ref, mport_release_mapping); 1347 } 1348 break; 1349 } 1350 } 1351 mutex_unlock(&md->buf_mutex); 1352 1353 return 0; 1354 } 1355 1356 /* 1357 * maint_port_idx_get() - Get the port index of the mport instance 1358 * @priv: driver private data 1359 * @arg: port index 1360 */ 1361 static int maint_port_idx_get(struct mport_cdev_priv *priv, void __user *arg) 1362 { 1363 struct mport_dev *md = priv->md; 1364 u32 port_idx = md->mport->index; 1365 1366 rmcd_debug(MPORT, "port_index=%d", port_idx); 1367 1368 if (copy_to_user(arg, &port_idx, sizeof(port_idx))) 1369 return -EFAULT; 1370 1371 return 0; 1372 } 1373 1374 static int rio_mport_add_event(struct mport_cdev_priv *priv, 1375 struct rio_event *event) 1376 { 1377 int overflow; 1378 1379 if (!(priv->event_mask & event->header)) 1380 return -EACCES; 1381 1382 spin_lock(&priv->fifo_lock); 1383 overflow = kfifo_avail(&priv->event_fifo) < sizeof(*event) 1384 || kfifo_in(&priv->event_fifo, (unsigned char *)event, 1385 sizeof(*event)) != sizeof(*event); 1386 spin_unlock(&priv->fifo_lock); 1387 1388 wake_up_interruptible(&priv->event_rx_wait); 1389 1390 if (overflow) { 1391 dev_warn(&priv->md->dev, DRV_NAME ": event fifo overflow\n"); 1392 return -EBUSY; 1393 } 1394 1395 return 0; 1396 } 1397 1398 static void rio_mport_doorbell_handler(struct rio_mport *mport, void *dev_id, 1399 u16 src, u16 dst, u16 info) 1400 { 1401 struct mport_dev *data = dev_id; 1402 struct mport_cdev_priv *priv; 1403 struct rio_mport_db_filter *db_filter; 1404 struct rio_event event; 1405 int handled; 1406 1407 event.header = RIO_DOORBELL; 1408 event.u.doorbell.rioid = src; 1409 event.u.doorbell.payload = info; 1410 1411 handled = 0; 1412 spin_lock(&data->db_lock); 1413 list_for_each_entry(db_filter, &data->doorbells, data_node) { 1414 if (((db_filter->filter.rioid == RIO_INVALID_DESTID || 1415 db_filter->filter.rioid == src)) && 1416 info >= db_filter->filter.low && 1417 info <= db_filter->filter.high) { 1418 priv = db_filter->priv; 1419 rio_mport_add_event(priv, &event); 1420 handled = 1; 1421 } 1422 } 1423 spin_unlock(&data->db_lock); 1424 1425 if (!handled) 1426 dev_warn(&data->dev, 1427 "%s: spurious DB received from 0x%x, info=0x%04x\n", 1428 __func__, src, info); 1429 } 1430 1431 static int rio_mport_add_db_filter(struct mport_cdev_priv *priv, 1432 void __user *arg) 1433 { 1434 struct mport_dev *md = priv->md; 1435 struct rio_mport_db_filter *db_filter; 1436 struct rio_doorbell_filter filter; 1437 unsigned long flags; 1438 int ret; 1439 1440 if (copy_from_user(&filter, arg, sizeof(filter))) 1441 return -EFAULT; 1442 1443 if (filter.low > filter.high) 1444 return -EINVAL; 1445 1446 ret = rio_request_inb_dbell(md->mport, md, filter.low, filter.high, 1447 rio_mport_doorbell_handler); 1448 if (ret) { 1449 rmcd_error("%s failed to register IBDB, err=%d", 1450 dev_name(&md->dev), ret); 1451 return ret; 1452 } 1453 1454 db_filter = kzalloc(sizeof(*db_filter), GFP_KERNEL); 1455 if (db_filter == NULL) { 1456 rio_release_inb_dbell(md->mport, filter.low, filter.high); 1457 return -ENOMEM; 1458 } 1459 1460 db_filter->filter = filter; 1461 db_filter->priv = priv; 1462 spin_lock_irqsave(&md->db_lock, flags); 1463 list_add_tail(&db_filter->priv_node, &priv->db_filters); 1464 list_add_tail(&db_filter->data_node, &md->doorbells); 1465 spin_unlock_irqrestore(&md->db_lock, flags); 1466 1467 return 0; 1468 } 1469 1470 static void rio_mport_delete_db_filter(struct rio_mport_db_filter *db_filter) 1471 { 1472 list_del(&db_filter->data_node); 1473 list_del(&db_filter->priv_node); 1474 kfree(db_filter); 1475 } 1476 1477 static int rio_mport_remove_db_filter(struct mport_cdev_priv *priv, 1478 void __user *arg) 1479 { 1480 struct rio_mport_db_filter *db_filter; 1481 struct rio_doorbell_filter filter; 1482 unsigned long flags; 1483 int ret = -EINVAL; 1484 1485 if (copy_from_user(&filter, arg, sizeof(filter))) 1486 return -EFAULT; 1487 1488 if (filter.low > filter.high) 1489 return -EINVAL; 1490 1491 spin_lock_irqsave(&priv->md->db_lock, flags); 1492 list_for_each_entry(db_filter, &priv->db_filters, priv_node) { 1493 if (db_filter->filter.rioid == filter.rioid && 1494 db_filter->filter.low == filter.low && 1495 db_filter->filter.high == filter.high) { 1496 rio_mport_delete_db_filter(db_filter); 1497 ret = 0; 1498 break; 1499 } 1500 } 1501 spin_unlock_irqrestore(&priv->md->db_lock, flags); 1502 1503 if (!ret) 1504 rio_release_inb_dbell(priv->md->mport, filter.low, filter.high); 1505 1506 return ret; 1507 } 1508 1509 static int rio_mport_match_pw(union rio_pw_msg *msg, 1510 struct rio_pw_filter *filter) 1511 { 1512 if ((msg->em.comptag & filter->mask) < filter->low || 1513 (msg->em.comptag & filter->mask) > filter->high) 1514 return 0; 1515 return 1; 1516 } 1517 1518 static int rio_mport_pw_handler(struct rio_mport *mport, void *context, 1519 union rio_pw_msg *msg, int step) 1520 { 1521 struct mport_dev *md = context; 1522 struct mport_cdev_priv *priv; 1523 struct rio_mport_pw_filter *pw_filter; 1524 struct rio_event event; 1525 int handled; 1526 1527 event.header = RIO_PORTWRITE; 1528 memcpy(event.u.portwrite.payload, msg->raw, RIO_PW_MSG_SIZE); 1529 1530 handled = 0; 1531 spin_lock(&md->pw_lock); 1532 list_for_each_entry(pw_filter, &md->portwrites, md_node) { 1533 if (rio_mport_match_pw(msg, &pw_filter->filter)) { 1534 priv = pw_filter->priv; 1535 rio_mport_add_event(priv, &event); 1536 handled = 1; 1537 } 1538 } 1539 spin_unlock(&md->pw_lock); 1540 1541 if (!handled) { 1542 printk_ratelimited(KERN_WARNING DRV_NAME 1543 ": mport%d received spurious PW from 0x%08x\n", 1544 mport->id, msg->em.comptag); 1545 } 1546 1547 return 0; 1548 } 1549 1550 static int rio_mport_add_pw_filter(struct mport_cdev_priv *priv, 1551 void __user *arg) 1552 { 1553 struct mport_dev *md = priv->md; 1554 struct rio_mport_pw_filter *pw_filter; 1555 struct rio_pw_filter filter; 1556 unsigned long flags; 1557 int hadd = 0; 1558 1559 if (copy_from_user(&filter, arg, sizeof(filter))) 1560 return -EFAULT; 1561 1562 pw_filter = kzalloc(sizeof(*pw_filter), GFP_KERNEL); 1563 if (pw_filter == NULL) 1564 return -ENOMEM; 1565 1566 pw_filter->filter = filter; 1567 pw_filter->priv = priv; 1568 spin_lock_irqsave(&md->pw_lock, flags); 1569 if (list_empty(&md->portwrites)) 1570 hadd = 1; 1571 list_add_tail(&pw_filter->priv_node, &priv->pw_filters); 1572 list_add_tail(&pw_filter->md_node, &md->portwrites); 1573 spin_unlock_irqrestore(&md->pw_lock, flags); 1574 1575 if (hadd) { 1576 int ret; 1577 1578 ret = rio_add_mport_pw_handler(md->mport, md, 1579 rio_mport_pw_handler); 1580 if (ret) { 1581 dev_err(&md->dev, 1582 "%s: failed to add IB_PW handler, err=%d\n", 1583 __func__, ret); 1584 return ret; 1585 } 1586 rio_pw_enable(md->mport, 1); 1587 } 1588 1589 return 0; 1590 } 1591 1592 static void rio_mport_delete_pw_filter(struct rio_mport_pw_filter *pw_filter) 1593 { 1594 list_del(&pw_filter->md_node); 1595 list_del(&pw_filter->priv_node); 1596 kfree(pw_filter); 1597 } 1598 1599 static int rio_mport_match_pw_filter(struct rio_pw_filter *a, 1600 struct rio_pw_filter *b) 1601 { 1602 if ((a->mask == b->mask) && (a->low == b->low) && (a->high == b->high)) 1603 return 1; 1604 return 0; 1605 } 1606 1607 static int rio_mport_remove_pw_filter(struct mport_cdev_priv *priv, 1608 void __user *arg) 1609 { 1610 struct mport_dev *md = priv->md; 1611 struct rio_mport_pw_filter *pw_filter; 1612 struct rio_pw_filter filter; 1613 unsigned long flags; 1614 int ret = -EINVAL; 1615 int hdel = 0; 1616 1617 if (copy_from_user(&filter, arg, sizeof(filter))) 1618 return -EFAULT; 1619 1620 spin_lock_irqsave(&md->pw_lock, flags); 1621 list_for_each_entry(pw_filter, &priv->pw_filters, priv_node) { 1622 if (rio_mport_match_pw_filter(&pw_filter->filter, &filter)) { 1623 rio_mport_delete_pw_filter(pw_filter); 1624 ret = 0; 1625 break; 1626 } 1627 } 1628 1629 if (list_empty(&md->portwrites)) 1630 hdel = 1; 1631 spin_unlock_irqrestore(&md->pw_lock, flags); 1632 1633 if (hdel) { 1634 rio_del_mport_pw_handler(md->mport, priv->md, 1635 rio_mport_pw_handler); 1636 rio_pw_enable(md->mport, 0); 1637 } 1638 1639 return ret; 1640 } 1641 1642 /* 1643 * rio_release_dev - release routine for kernel RIO device object 1644 * @dev: kernel device object associated with a RIO device structure 1645 * 1646 * Frees a RIO device struct associated a RIO device struct. 1647 * The RIO device struct is freed. 1648 */ 1649 static void rio_release_dev(struct device *dev) 1650 { 1651 struct rio_dev *rdev; 1652 1653 rdev = to_rio_dev(dev); 1654 pr_info(DRV_PREFIX "%s: %s\n", __func__, rio_name(rdev)); 1655 kfree(rdev); 1656 } 1657 1658 1659 static void rio_release_net(struct device *dev) 1660 { 1661 struct rio_net *net; 1662 1663 net = to_rio_net(dev); 1664 rmcd_debug(RDEV, "net_%d", net->id); 1665 kfree(net); 1666 } 1667 1668 1669 /* 1670 * rio_mport_add_riodev - creates a kernel RIO device object 1671 * 1672 * Allocates a RIO device data structure and initializes required fields based 1673 * on device's configuration space contents. 1674 * If the device has switch capabilities, then a switch specific portion is 1675 * allocated and configured. 1676 */ 1677 static int rio_mport_add_riodev(struct mport_cdev_priv *priv, 1678 void __user *arg) 1679 { 1680 struct mport_dev *md = priv->md; 1681 struct rio_rdev_info dev_info; 1682 struct rio_dev *rdev; 1683 struct rio_switch *rswitch = NULL; 1684 struct rio_mport *mport; 1685 size_t size; 1686 u32 rval; 1687 u32 swpinfo = 0; 1688 u16 destid; 1689 u8 hopcount; 1690 int err; 1691 1692 if (copy_from_user(&dev_info, arg, sizeof(dev_info))) 1693 return -EFAULT; 1694 1695 rmcd_debug(RDEV, "name:%s ct:0x%x did:0x%x hc:0x%x", dev_info.name, 1696 dev_info.comptag, dev_info.destid, dev_info.hopcount); 1697 1698 if (bus_find_device_by_name(&rio_bus_type, NULL, dev_info.name)) { 1699 rmcd_debug(RDEV, "device %s already exists", dev_info.name); 1700 return -EEXIST; 1701 } 1702 1703 size = sizeof(*rdev); 1704 mport = md->mport; 1705 destid = dev_info.destid; 1706 hopcount = dev_info.hopcount; 1707 1708 if (rio_mport_read_config_32(mport, destid, hopcount, 1709 RIO_PEF_CAR, &rval)) 1710 return -EIO; 1711 1712 if (rval & RIO_PEF_SWITCH) { 1713 rio_mport_read_config_32(mport, destid, hopcount, 1714 RIO_SWP_INFO_CAR, &swpinfo); 1715 size += (RIO_GET_TOTAL_PORTS(swpinfo) * 1716 sizeof(rswitch->nextdev[0])) + sizeof(*rswitch); 1717 } 1718 1719 rdev = kzalloc(size, GFP_KERNEL); 1720 if (rdev == NULL) 1721 return -ENOMEM; 1722 1723 if (mport->net == NULL) { 1724 struct rio_net *net; 1725 1726 net = rio_alloc_net(mport); 1727 if (!net) { 1728 err = -ENOMEM; 1729 rmcd_debug(RDEV, "failed to allocate net object"); 1730 goto cleanup; 1731 } 1732 1733 net->id = mport->id; 1734 net->hport = mport; 1735 dev_set_name(&net->dev, "rnet_%d", net->id); 1736 net->dev.parent = &mport->dev; 1737 net->dev.release = rio_release_net; 1738 err = rio_add_net(net); 1739 if (err) { 1740 rmcd_debug(RDEV, "failed to register net, err=%d", err); 1741 kfree(net); 1742 goto cleanup; 1743 } 1744 } 1745 1746 rdev->net = mport->net; 1747 rdev->pef = rval; 1748 rdev->swpinfo = swpinfo; 1749 rio_mport_read_config_32(mport, destid, hopcount, 1750 RIO_DEV_ID_CAR, &rval); 1751 rdev->did = rval >> 16; 1752 rdev->vid = rval & 0xffff; 1753 rio_mport_read_config_32(mport, destid, hopcount, RIO_DEV_INFO_CAR, 1754 &rdev->device_rev); 1755 rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_ID_CAR, 1756 &rval); 1757 rdev->asm_did = rval >> 16; 1758 rdev->asm_vid = rval & 0xffff; 1759 rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_INFO_CAR, 1760 &rval); 1761 rdev->asm_rev = rval >> 16; 1762 1763 if (rdev->pef & RIO_PEF_EXT_FEATURES) { 1764 rdev->efptr = rval & 0xffff; 1765 rdev->phys_efptr = rio_mport_get_physefb(mport, 0, destid, 1766 hopcount, &rdev->phys_rmap); 1767 1768 rdev->em_efptr = rio_mport_get_feature(mport, 0, destid, 1769 hopcount, RIO_EFB_ERR_MGMNT); 1770 } 1771 1772 rio_mport_read_config_32(mport, destid, hopcount, RIO_SRC_OPS_CAR, 1773 &rdev->src_ops); 1774 rio_mport_read_config_32(mport, destid, hopcount, RIO_DST_OPS_CAR, 1775 &rdev->dst_ops); 1776 1777 rdev->comp_tag = dev_info.comptag; 1778 rdev->destid = destid; 1779 /* hopcount is stored as specified by a caller, regardles of EP or SW */ 1780 rdev->hopcount = hopcount; 1781 1782 if (rdev->pef & RIO_PEF_SWITCH) { 1783 rswitch = rdev->rswitch; 1784 rswitch->route_table = NULL; 1785 } 1786 1787 if (strlen(dev_info.name)) 1788 dev_set_name(&rdev->dev, "%s", dev_info.name); 1789 else if (rdev->pef & RIO_PEF_SWITCH) 1790 dev_set_name(&rdev->dev, "%02x:s:%04x", mport->id, 1791 rdev->comp_tag & RIO_CTAG_UDEVID); 1792 else 1793 dev_set_name(&rdev->dev, "%02x:e:%04x", mport->id, 1794 rdev->comp_tag & RIO_CTAG_UDEVID); 1795 1796 INIT_LIST_HEAD(&rdev->net_list); 1797 rdev->dev.parent = &mport->net->dev; 1798 rio_attach_device(rdev); 1799 rdev->dev.release = rio_release_dev; 1800 1801 if (rdev->dst_ops & RIO_DST_OPS_DOORBELL) 1802 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE], 1803 0, 0xffff); 1804 err = rio_add_device(rdev); 1805 if (err) 1806 goto cleanup; 1807 rio_dev_get(rdev); 1808 1809 return 0; 1810 cleanup: 1811 kfree(rdev); 1812 return err; 1813 } 1814 1815 static int rio_mport_del_riodev(struct mport_cdev_priv *priv, void __user *arg) 1816 { 1817 struct rio_rdev_info dev_info; 1818 struct rio_dev *rdev = NULL; 1819 struct device *dev; 1820 struct rio_mport *mport; 1821 struct rio_net *net; 1822 1823 if (copy_from_user(&dev_info, arg, sizeof(dev_info))) 1824 return -EFAULT; 1825 1826 mport = priv->md->mport; 1827 1828 /* If device name is specified, removal by name has priority */ 1829 if (strlen(dev_info.name)) { 1830 dev = bus_find_device_by_name(&rio_bus_type, NULL, 1831 dev_info.name); 1832 if (dev) 1833 rdev = to_rio_dev(dev); 1834 } else { 1835 do { 1836 rdev = rio_get_comptag(dev_info.comptag, rdev); 1837 if (rdev && rdev->dev.parent == &mport->net->dev && 1838 rdev->destid == dev_info.destid && 1839 rdev->hopcount == dev_info.hopcount) 1840 break; 1841 } while (rdev); 1842 } 1843 1844 if (!rdev) { 1845 rmcd_debug(RDEV, 1846 "device name:%s ct:0x%x did:0x%x hc:0x%x not found", 1847 dev_info.name, dev_info.comptag, dev_info.destid, 1848 dev_info.hopcount); 1849 return -ENODEV; 1850 } 1851 1852 net = rdev->net; 1853 rio_dev_put(rdev); 1854 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN); 1855 1856 if (list_empty(&net->devices)) { 1857 rio_free_net(net); 1858 mport->net = NULL; 1859 } 1860 1861 return 0; 1862 } 1863 1864 /* 1865 * Mport cdev management 1866 */ 1867 1868 /* 1869 * mport_cdev_open() - Open character device (mport) 1870 */ 1871 static int mport_cdev_open(struct inode *inode, struct file *filp) 1872 { 1873 int ret; 1874 int minor = iminor(inode); 1875 struct mport_dev *chdev; 1876 struct mport_cdev_priv *priv; 1877 1878 /* Test for valid device */ 1879 if (minor >= RIO_MAX_MPORTS) { 1880 rmcd_error("Invalid minor device number"); 1881 return -EINVAL; 1882 } 1883 1884 chdev = container_of(inode->i_cdev, struct mport_dev, cdev); 1885 1886 rmcd_debug(INIT, "%s filp=%p", dev_name(&chdev->dev), filp); 1887 1888 if (atomic_read(&chdev->active) == 0) 1889 return -ENODEV; 1890 1891 get_device(&chdev->dev); 1892 1893 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 1894 if (!priv) { 1895 put_device(&chdev->dev); 1896 return -ENOMEM; 1897 } 1898 1899 priv->md = chdev; 1900 1901 mutex_lock(&chdev->file_mutex); 1902 list_add_tail(&priv->list, &chdev->file_list); 1903 mutex_unlock(&chdev->file_mutex); 1904 1905 INIT_LIST_HEAD(&priv->db_filters); 1906 INIT_LIST_HEAD(&priv->pw_filters); 1907 spin_lock_init(&priv->fifo_lock); 1908 init_waitqueue_head(&priv->event_rx_wait); 1909 ret = kfifo_alloc(&priv->event_fifo, 1910 sizeof(struct rio_event) * MPORT_EVENT_DEPTH, 1911 GFP_KERNEL); 1912 if (ret < 0) { 1913 dev_err(&chdev->dev, DRV_NAME ": kfifo_alloc failed\n"); 1914 ret = -ENOMEM; 1915 goto err_fifo; 1916 } 1917 1918 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 1919 INIT_LIST_HEAD(&priv->async_list); 1920 spin_lock_init(&priv->req_lock); 1921 mutex_init(&priv->dma_lock); 1922 #endif 1923 1924 filp->private_data = priv; 1925 goto out; 1926 err_fifo: 1927 kfree(priv); 1928 out: 1929 return ret; 1930 } 1931 1932 static int mport_cdev_fasync(int fd, struct file *filp, int mode) 1933 { 1934 struct mport_cdev_priv *priv = filp->private_data; 1935 1936 return fasync_helper(fd, filp, mode, &priv->async_queue); 1937 } 1938 1939 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 1940 static void mport_cdev_release_dma(struct file *filp) 1941 { 1942 struct mport_cdev_priv *priv = filp->private_data; 1943 struct mport_dev *md; 1944 struct mport_dma_req *req, *req_next; 1945 unsigned long tmo = msecs_to_jiffies(dma_timeout); 1946 long wret; 1947 LIST_HEAD(list); 1948 1949 rmcd_debug(EXIT, "from filp=%p %s(%d)", 1950 filp, current->comm, task_pid_nr(current)); 1951 1952 if (!priv->dmach) { 1953 rmcd_debug(EXIT, "No DMA channel for filp=%p", filp); 1954 return; 1955 } 1956 1957 md = priv->md; 1958 1959 spin_lock(&priv->req_lock); 1960 if (!list_empty(&priv->async_list)) { 1961 rmcd_debug(EXIT, "async list not empty filp=%p %s(%d)", 1962 filp, current->comm, task_pid_nr(current)); 1963 list_splice_init(&priv->async_list, &list); 1964 } 1965 spin_unlock(&priv->req_lock); 1966 1967 if (!list_empty(&list)) { 1968 rmcd_debug(EXIT, "temp list not empty"); 1969 list_for_each_entry_safe(req, req_next, &list, node) { 1970 rmcd_debug(EXIT, "free req->filp=%p cookie=%d compl=%s", 1971 req->filp, req->cookie, 1972 completion_done(&req->req_comp)?"yes":"no"); 1973 list_del(&req->node); 1974 kref_put(&req->refcount, dma_req_free); 1975 } 1976 } 1977 1978 put_dma_channel(priv); 1979 wret = wait_for_completion_interruptible_timeout(&priv->comp, tmo); 1980 1981 if (wret <= 0) { 1982 rmcd_error("%s(%d) failed waiting for DMA release err=%ld", 1983 current->comm, task_pid_nr(current), wret); 1984 } 1985 1986 if (priv->dmach != priv->md->dma_chan) { 1987 rmcd_debug(EXIT, "Release DMA channel for filp=%p %s(%d)", 1988 filp, current->comm, task_pid_nr(current)); 1989 rio_release_dma(priv->dmach); 1990 } else { 1991 rmcd_debug(EXIT, "Adjust default DMA channel refcount"); 1992 kref_put(&md->dma_ref, mport_release_def_dma); 1993 } 1994 1995 priv->dmach = NULL; 1996 } 1997 #else 1998 #define mport_cdev_release_dma(priv) do {} while (0) 1999 #endif 2000 2001 /* 2002 * mport_cdev_release() - Release character device 2003 */ 2004 static int mport_cdev_release(struct inode *inode, struct file *filp) 2005 { 2006 struct mport_cdev_priv *priv = filp->private_data; 2007 struct mport_dev *chdev; 2008 struct rio_mport_pw_filter *pw_filter, *pw_filter_next; 2009 struct rio_mport_db_filter *db_filter, *db_filter_next; 2010 struct rio_mport_mapping *map, *_map; 2011 unsigned long flags; 2012 2013 rmcd_debug(EXIT, "%s filp=%p", dev_name(&priv->md->dev), filp); 2014 2015 chdev = priv->md; 2016 mport_cdev_release_dma(filp); 2017 2018 priv->event_mask = 0; 2019 2020 spin_lock_irqsave(&chdev->pw_lock, flags); 2021 if (!list_empty(&priv->pw_filters)) { 2022 list_for_each_entry_safe(pw_filter, pw_filter_next, 2023 &priv->pw_filters, priv_node) 2024 rio_mport_delete_pw_filter(pw_filter); 2025 } 2026 spin_unlock_irqrestore(&chdev->pw_lock, flags); 2027 2028 spin_lock_irqsave(&chdev->db_lock, flags); 2029 list_for_each_entry_safe(db_filter, db_filter_next, 2030 &priv->db_filters, priv_node) { 2031 rio_mport_delete_db_filter(db_filter); 2032 } 2033 spin_unlock_irqrestore(&chdev->db_lock, flags); 2034 2035 kfifo_free(&priv->event_fifo); 2036 2037 mutex_lock(&chdev->buf_mutex); 2038 list_for_each_entry_safe(map, _map, &chdev->mappings, node) { 2039 if (map->filp == filp) { 2040 rmcd_debug(EXIT, "release mapping %p filp=%p", 2041 map->virt_addr, filp); 2042 kref_put(&map->ref, mport_release_mapping); 2043 } 2044 } 2045 mutex_unlock(&chdev->buf_mutex); 2046 2047 mport_cdev_fasync(-1, filp, 0); 2048 filp->private_data = NULL; 2049 mutex_lock(&chdev->file_mutex); 2050 list_del(&priv->list); 2051 mutex_unlock(&chdev->file_mutex); 2052 put_device(&chdev->dev); 2053 kfree(priv); 2054 return 0; 2055 } 2056 2057 /* 2058 * mport_cdev_ioctl() - IOCTLs for character device 2059 */ 2060 static long mport_cdev_ioctl(struct file *filp, 2061 unsigned int cmd, unsigned long arg) 2062 { 2063 int err = -EINVAL; 2064 struct mport_cdev_priv *data = filp->private_data; 2065 struct mport_dev *md = data->md; 2066 2067 if (atomic_read(&md->active) == 0) 2068 return -ENODEV; 2069 2070 switch (cmd) { 2071 case RIO_MPORT_MAINT_READ_LOCAL: 2072 return rio_mport_maint_rd(data, (void __user *)arg, 1); 2073 case RIO_MPORT_MAINT_WRITE_LOCAL: 2074 return rio_mport_maint_wr(data, (void __user *)arg, 1); 2075 case RIO_MPORT_MAINT_READ_REMOTE: 2076 return rio_mport_maint_rd(data, (void __user *)arg, 0); 2077 case RIO_MPORT_MAINT_WRITE_REMOTE: 2078 return rio_mport_maint_wr(data, (void __user *)arg, 0); 2079 case RIO_MPORT_MAINT_HDID_SET: 2080 return maint_hdid_set(data, (void __user *)arg); 2081 case RIO_MPORT_MAINT_COMPTAG_SET: 2082 return maint_comptag_set(data, (void __user *)arg); 2083 case RIO_MPORT_MAINT_PORT_IDX_GET: 2084 return maint_port_idx_get(data, (void __user *)arg); 2085 case RIO_MPORT_GET_PROPERTIES: 2086 md->properties.hdid = md->mport->host_deviceid; 2087 if (copy_to_user((void __user *)arg, &(md->properties), 2088 sizeof(md->properties))) 2089 return -EFAULT; 2090 return 0; 2091 case RIO_ENABLE_DOORBELL_RANGE: 2092 return rio_mport_add_db_filter(data, (void __user *)arg); 2093 case RIO_DISABLE_DOORBELL_RANGE: 2094 return rio_mport_remove_db_filter(data, (void __user *)arg); 2095 case RIO_ENABLE_PORTWRITE_RANGE: 2096 return rio_mport_add_pw_filter(data, (void __user *)arg); 2097 case RIO_DISABLE_PORTWRITE_RANGE: 2098 return rio_mport_remove_pw_filter(data, (void __user *)arg); 2099 case RIO_SET_EVENT_MASK: 2100 data->event_mask = (u32)arg; 2101 return 0; 2102 case RIO_GET_EVENT_MASK: 2103 if (copy_to_user((void __user *)arg, &data->event_mask, 2104 sizeof(u32))) 2105 return -EFAULT; 2106 return 0; 2107 case RIO_MAP_OUTBOUND: 2108 return rio_mport_obw_map(filp, (void __user *)arg); 2109 case RIO_MAP_INBOUND: 2110 return rio_mport_map_inbound(filp, (void __user *)arg); 2111 case RIO_UNMAP_OUTBOUND: 2112 return rio_mport_obw_free(filp, (void __user *)arg); 2113 case RIO_UNMAP_INBOUND: 2114 return rio_mport_inbound_free(filp, (void __user *)arg); 2115 case RIO_ALLOC_DMA: 2116 return rio_mport_alloc_dma(filp, (void __user *)arg); 2117 case RIO_FREE_DMA: 2118 return rio_mport_free_dma(filp, (void __user *)arg); 2119 case RIO_WAIT_FOR_ASYNC: 2120 return rio_mport_wait_for_async_dma(filp, (void __user *)arg); 2121 case RIO_TRANSFER: 2122 return rio_mport_transfer_ioctl(filp, (void __user *)arg); 2123 case RIO_DEV_ADD: 2124 return rio_mport_add_riodev(data, (void __user *)arg); 2125 case RIO_DEV_DEL: 2126 return rio_mport_del_riodev(data, (void __user *)arg); 2127 default: 2128 break; 2129 } 2130 2131 return err; 2132 } 2133 2134 /* 2135 * mport_release_mapping - free mapping resources and info structure 2136 * @ref: a pointer to the kref within struct rio_mport_mapping 2137 * 2138 * NOTE: Shall be called while holding buf_mutex. 2139 */ 2140 static void mport_release_mapping(struct kref *ref) 2141 { 2142 struct rio_mport_mapping *map = 2143 container_of(ref, struct rio_mport_mapping, ref); 2144 struct rio_mport *mport = map->md->mport; 2145 2146 rmcd_debug(MMAP, "type %d mapping @ %p (phys = %pad) for %s", 2147 map->dir, map->virt_addr, 2148 &map->phys_addr, mport->name); 2149 2150 list_del(&map->node); 2151 2152 switch (map->dir) { 2153 case MAP_INBOUND: 2154 rio_unmap_inb_region(mport, map->phys_addr); 2155 case MAP_DMA: 2156 dma_free_coherent(mport->dev.parent, map->size, 2157 map->virt_addr, map->phys_addr); 2158 break; 2159 case MAP_OUTBOUND: 2160 rio_unmap_outb_region(mport, map->rioid, map->rio_addr); 2161 break; 2162 } 2163 kfree(map); 2164 } 2165 2166 static void mport_mm_open(struct vm_area_struct *vma) 2167 { 2168 struct rio_mport_mapping *map = vma->vm_private_data; 2169 2170 rmcd_debug(MMAP, "%pad", &map->phys_addr); 2171 kref_get(&map->ref); 2172 } 2173 2174 static void mport_mm_close(struct vm_area_struct *vma) 2175 { 2176 struct rio_mport_mapping *map = vma->vm_private_data; 2177 2178 rmcd_debug(MMAP, "%pad", &map->phys_addr); 2179 mutex_lock(&map->md->buf_mutex); 2180 kref_put(&map->ref, mport_release_mapping); 2181 mutex_unlock(&map->md->buf_mutex); 2182 } 2183 2184 static const struct vm_operations_struct vm_ops = { 2185 .open = mport_mm_open, 2186 .close = mport_mm_close, 2187 }; 2188 2189 static int mport_cdev_mmap(struct file *filp, struct vm_area_struct *vma) 2190 { 2191 struct mport_cdev_priv *priv = filp->private_data; 2192 struct mport_dev *md; 2193 size_t size = vma->vm_end - vma->vm_start; 2194 dma_addr_t baddr; 2195 unsigned long offset; 2196 int found = 0, ret; 2197 struct rio_mport_mapping *map; 2198 2199 rmcd_debug(MMAP, "0x%x bytes at offset 0x%lx", 2200 (unsigned int)size, vma->vm_pgoff); 2201 2202 md = priv->md; 2203 baddr = ((dma_addr_t)vma->vm_pgoff << PAGE_SHIFT); 2204 2205 mutex_lock(&md->buf_mutex); 2206 list_for_each_entry(map, &md->mappings, node) { 2207 if (baddr >= map->phys_addr && 2208 baddr < (map->phys_addr + map->size)) { 2209 found = 1; 2210 break; 2211 } 2212 } 2213 mutex_unlock(&md->buf_mutex); 2214 2215 if (!found) 2216 return -ENOMEM; 2217 2218 offset = baddr - map->phys_addr; 2219 2220 if (size + offset > map->size) 2221 return -EINVAL; 2222 2223 vma->vm_pgoff = offset >> PAGE_SHIFT; 2224 rmcd_debug(MMAP, "MMAP adjusted offset = 0x%lx", vma->vm_pgoff); 2225 2226 if (map->dir == MAP_INBOUND || map->dir == MAP_DMA) 2227 ret = dma_mmap_coherent(md->mport->dev.parent, vma, 2228 map->virt_addr, map->phys_addr, map->size); 2229 else if (map->dir == MAP_OUTBOUND) { 2230 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 2231 ret = vm_iomap_memory(vma, map->phys_addr, map->size); 2232 } else { 2233 rmcd_error("Attempt to mmap unsupported mapping type"); 2234 ret = -EIO; 2235 } 2236 2237 if (!ret) { 2238 vma->vm_private_data = map; 2239 vma->vm_ops = &vm_ops; 2240 mport_mm_open(vma); 2241 } else { 2242 rmcd_error("MMAP exit with err=%d", ret); 2243 } 2244 2245 return ret; 2246 } 2247 2248 static __poll_t mport_cdev_poll(struct file *filp, poll_table *wait) 2249 { 2250 struct mport_cdev_priv *priv = filp->private_data; 2251 2252 poll_wait(filp, &priv->event_rx_wait, wait); 2253 if (kfifo_len(&priv->event_fifo)) 2254 return EPOLLIN | EPOLLRDNORM; 2255 2256 return 0; 2257 } 2258 2259 static ssize_t mport_read(struct file *filp, char __user *buf, size_t count, 2260 loff_t *ppos) 2261 { 2262 struct mport_cdev_priv *priv = filp->private_data; 2263 int copied; 2264 ssize_t ret; 2265 2266 if (!count) 2267 return 0; 2268 2269 if (kfifo_is_empty(&priv->event_fifo) && 2270 (filp->f_flags & O_NONBLOCK)) 2271 return -EAGAIN; 2272 2273 if (count % sizeof(struct rio_event)) 2274 return -EINVAL; 2275 2276 ret = wait_event_interruptible(priv->event_rx_wait, 2277 kfifo_len(&priv->event_fifo) != 0); 2278 if (ret) 2279 return ret; 2280 2281 while (ret < count) { 2282 if (kfifo_to_user(&priv->event_fifo, buf, 2283 sizeof(struct rio_event), &copied)) 2284 return -EFAULT; 2285 ret += copied; 2286 buf += copied; 2287 } 2288 2289 return ret; 2290 } 2291 2292 static ssize_t mport_write(struct file *filp, const char __user *buf, 2293 size_t count, loff_t *ppos) 2294 { 2295 struct mport_cdev_priv *priv = filp->private_data; 2296 struct rio_mport *mport = priv->md->mport; 2297 struct rio_event event; 2298 int len, ret; 2299 2300 if (!count) 2301 return 0; 2302 2303 if (count % sizeof(event)) 2304 return -EINVAL; 2305 2306 len = 0; 2307 while ((count - len) >= (int)sizeof(event)) { 2308 if (copy_from_user(&event, buf, sizeof(event))) 2309 return -EFAULT; 2310 2311 if (event.header != RIO_DOORBELL) 2312 return -EINVAL; 2313 2314 ret = rio_mport_send_doorbell(mport, 2315 event.u.doorbell.rioid, 2316 event.u.doorbell.payload); 2317 if (ret < 0) 2318 return ret; 2319 2320 len += sizeof(event); 2321 buf += sizeof(event); 2322 } 2323 2324 return len; 2325 } 2326 2327 static const struct file_operations mport_fops = { 2328 .owner = THIS_MODULE, 2329 .open = mport_cdev_open, 2330 .release = mport_cdev_release, 2331 .poll = mport_cdev_poll, 2332 .read = mport_read, 2333 .write = mport_write, 2334 .mmap = mport_cdev_mmap, 2335 .fasync = mport_cdev_fasync, 2336 .unlocked_ioctl = mport_cdev_ioctl 2337 }; 2338 2339 /* 2340 * Character device management 2341 */ 2342 2343 static void mport_device_release(struct device *dev) 2344 { 2345 struct mport_dev *md; 2346 2347 rmcd_debug(EXIT, "%s", dev_name(dev)); 2348 md = container_of(dev, struct mport_dev, dev); 2349 kfree(md); 2350 } 2351 2352 /* 2353 * mport_cdev_add() - Create mport_dev from rio_mport 2354 * @mport: RapidIO master port 2355 */ 2356 static struct mport_dev *mport_cdev_add(struct rio_mport *mport) 2357 { 2358 int ret = 0; 2359 struct mport_dev *md; 2360 struct rio_mport_attr attr; 2361 2362 md = kzalloc(sizeof(*md), GFP_KERNEL); 2363 if (!md) { 2364 rmcd_error("Unable allocate a device object"); 2365 return NULL; 2366 } 2367 2368 md->mport = mport; 2369 mutex_init(&md->buf_mutex); 2370 mutex_init(&md->file_mutex); 2371 INIT_LIST_HEAD(&md->file_list); 2372 2373 device_initialize(&md->dev); 2374 md->dev.devt = MKDEV(MAJOR(dev_number), mport->id); 2375 md->dev.class = dev_class; 2376 md->dev.parent = &mport->dev; 2377 md->dev.release = mport_device_release; 2378 dev_set_name(&md->dev, DEV_NAME "%d", mport->id); 2379 atomic_set(&md->active, 1); 2380 2381 cdev_init(&md->cdev, &mport_fops); 2382 md->cdev.owner = THIS_MODULE; 2383 2384 ret = cdev_device_add(&md->cdev, &md->dev); 2385 if (ret) { 2386 rmcd_error("Failed to register mport %d (err=%d)", 2387 mport->id, ret); 2388 goto err_cdev; 2389 } 2390 2391 INIT_LIST_HEAD(&md->doorbells); 2392 spin_lock_init(&md->db_lock); 2393 INIT_LIST_HEAD(&md->portwrites); 2394 spin_lock_init(&md->pw_lock); 2395 INIT_LIST_HEAD(&md->mappings); 2396 2397 md->properties.id = mport->id; 2398 md->properties.sys_size = mport->sys_size; 2399 md->properties.hdid = mport->host_deviceid; 2400 md->properties.index = mport->index; 2401 2402 /* The transfer_mode property will be returned through mport query 2403 * interface 2404 */ 2405 #ifdef CONFIG_FSL_RIO /* for now: only on Freescale's SoCs */ 2406 md->properties.transfer_mode |= RIO_TRANSFER_MODE_MAPPED; 2407 #else 2408 md->properties.transfer_mode |= RIO_TRANSFER_MODE_TRANSFER; 2409 #endif 2410 ret = rio_query_mport(mport, &attr); 2411 if (!ret) { 2412 md->properties.flags = attr.flags; 2413 md->properties.link_speed = attr.link_speed; 2414 md->properties.link_width = attr.link_width; 2415 md->properties.dma_max_sge = attr.dma_max_sge; 2416 md->properties.dma_max_size = attr.dma_max_size; 2417 md->properties.dma_align = attr.dma_align; 2418 md->properties.cap_sys_size = 0; 2419 md->properties.cap_transfer_mode = 0; 2420 md->properties.cap_addr_size = 0; 2421 } else 2422 pr_info(DRV_PREFIX "Failed to obtain info for %s cdev(%d:%d)\n", 2423 mport->name, MAJOR(dev_number), mport->id); 2424 2425 mutex_lock(&mport_devs_lock); 2426 list_add_tail(&md->node, &mport_devs); 2427 mutex_unlock(&mport_devs_lock); 2428 2429 pr_info(DRV_PREFIX "Added %s cdev(%d:%d)\n", 2430 mport->name, MAJOR(dev_number), mport->id); 2431 2432 return md; 2433 2434 err_cdev: 2435 put_device(&md->dev); 2436 return NULL; 2437 } 2438 2439 /* 2440 * mport_cdev_terminate_dma() - Stop all active DMA data transfers and release 2441 * associated DMA channels. 2442 */ 2443 static void mport_cdev_terminate_dma(struct mport_dev *md) 2444 { 2445 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 2446 struct mport_cdev_priv *client; 2447 2448 rmcd_debug(DMA, "%s", dev_name(&md->dev)); 2449 2450 mutex_lock(&md->file_mutex); 2451 list_for_each_entry(client, &md->file_list, list) { 2452 if (client->dmach) { 2453 dmaengine_terminate_all(client->dmach); 2454 rio_release_dma(client->dmach); 2455 } 2456 } 2457 mutex_unlock(&md->file_mutex); 2458 2459 if (md->dma_chan) { 2460 dmaengine_terminate_all(md->dma_chan); 2461 rio_release_dma(md->dma_chan); 2462 md->dma_chan = NULL; 2463 } 2464 #endif 2465 } 2466 2467 2468 /* 2469 * mport_cdev_kill_fasync() - Send SIGIO signal to all processes with open 2470 * mport_cdev files. 2471 */ 2472 static int mport_cdev_kill_fasync(struct mport_dev *md) 2473 { 2474 unsigned int files = 0; 2475 struct mport_cdev_priv *client; 2476 2477 mutex_lock(&md->file_mutex); 2478 list_for_each_entry(client, &md->file_list, list) { 2479 if (client->async_queue) 2480 kill_fasync(&client->async_queue, SIGIO, POLL_HUP); 2481 files++; 2482 } 2483 mutex_unlock(&md->file_mutex); 2484 return files; 2485 } 2486 2487 /* 2488 * mport_cdev_remove() - Remove mport character device 2489 * @dev: Mport device to remove 2490 */ 2491 static void mport_cdev_remove(struct mport_dev *md) 2492 { 2493 struct rio_mport_mapping *map, *_map; 2494 2495 rmcd_debug(EXIT, "Remove %s cdev", md->mport->name); 2496 atomic_set(&md->active, 0); 2497 mport_cdev_terminate_dma(md); 2498 rio_del_mport_pw_handler(md->mport, md, rio_mport_pw_handler); 2499 cdev_device_del(&md->cdev, &md->dev); 2500 mport_cdev_kill_fasync(md); 2501 2502 /* TODO: do we need to give clients some time to close file 2503 * descriptors? Simple wait for XX, or kref? 2504 */ 2505 2506 /* 2507 * Release DMA buffers allocated for the mport device. 2508 * Disable associated inbound Rapidio requests mapping if applicable. 2509 */ 2510 mutex_lock(&md->buf_mutex); 2511 list_for_each_entry_safe(map, _map, &md->mappings, node) { 2512 kref_put(&map->ref, mport_release_mapping); 2513 } 2514 mutex_unlock(&md->buf_mutex); 2515 2516 if (!list_empty(&md->mappings)) 2517 rmcd_warn("WARNING: %s pending mappings on removal", 2518 md->mport->name); 2519 2520 rio_release_inb_dbell(md->mport, 0, 0x0fff); 2521 2522 put_device(&md->dev); 2523 } 2524 2525 /* 2526 * RIO rio_mport_interface driver 2527 */ 2528 2529 /* 2530 * mport_add_mport() - Add rio_mport from LDM device struct 2531 * @dev: Linux device model struct 2532 * @class_intf: Linux class_interface 2533 */ 2534 static int mport_add_mport(struct device *dev, 2535 struct class_interface *class_intf) 2536 { 2537 struct rio_mport *mport = NULL; 2538 struct mport_dev *chdev = NULL; 2539 2540 mport = to_rio_mport(dev); 2541 if (!mport) 2542 return -ENODEV; 2543 2544 chdev = mport_cdev_add(mport); 2545 if (!chdev) 2546 return -ENODEV; 2547 2548 return 0; 2549 } 2550 2551 /* 2552 * mport_remove_mport() - Remove rio_mport from global list 2553 * TODO remove device from global mport_dev list 2554 */ 2555 static void mport_remove_mport(struct device *dev, 2556 struct class_interface *class_intf) 2557 { 2558 struct rio_mport *mport = NULL; 2559 struct mport_dev *chdev; 2560 int found = 0; 2561 2562 mport = to_rio_mport(dev); 2563 rmcd_debug(EXIT, "Remove %s", mport->name); 2564 2565 mutex_lock(&mport_devs_lock); 2566 list_for_each_entry(chdev, &mport_devs, node) { 2567 if (chdev->mport->id == mport->id) { 2568 atomic_set(&chdev->active, 0); 2569 list_del(&chdev->node); 2570 found = 1; 2571 break; 2572 } 2573 } 2574 mutex_unlock(&mport_devs_lock); 2575 2576 if (found) 2577 mport_cdev_remove(chdev); 2578 } 2579 2580 /* the rio_mport_interface is used to handle local mport devices */ 2581 static struct class_interface rio_mport_interface __refdata = { 2582 .class = &rio_mport_class, 2583 .add_dev = mport_add_mport, 2584 .remove_dev = mport_remove_mport, 2585 }; 2586 2587 /* 2588 * Linux kernel module 2589 */ 2590 2591 /* 2592 * mport_init - Driver module loading 2593 */ 2594 static int __init mport_init(void) 2595 { 2596 int ret; 2597 2598 /* Create device class needed by udev */ 2599 dev_class = class_create(THIS_MODULE, DRV_NAME); 2600 if (IS_ERR(dev_class)) { 2601 rmcd_error("Unable to create " DRV_NAME " class"); 2602 return PTR_ERR(dev_class); 2603 } 2604 2605 ret = alloc_chrdev_region(&dev_number, 0, RIO_MAX_MPORTS, DRV_NAME); 2606 if (ret < 0) 2607 goto err_chr; 2608 2609 rmcd_debug(INIT, "Registered class with major=%d", MAJOR(dev_number)); 2610 2611 /* Register to rio_mport_interface */ 2612 ret = class_interface_register(&rio_mport_interface); 2613 if (ret) { 2614 rmcd_error("class_interface_register() failed, err=%d", ret); 2615 goto err_cli; 2616 } 2617 2618 return 0; 2619 2620 err_cli: 2621 unregister_chrdev_region(dev_number, RIO_MAX_MPORTS); 2622 err_chr: 2623 class_destroy(dev_class); 2624 return ret; 2625 } 2626 2627 /** 2628 * mport_exit - Driver module unloading 2629 */ 2630 static void __exit mport_exit(void) 2631 { 2632 class_interface_unregister(&rio_mport_interface); 2633 class_destroy(dev_class); 2634 unregister_chrdev_region(dev_number, RIO_MAX_MPORTS); 2635 } 2636 2637 module_init(mport_init); 2638 module_exit(mport_exit); 2639