blob: 0434ab7b649709cd6fcbf5192ca697b6f0148982 [file] [log] [blame]
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001/*
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 */
54enum {
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
86MODULE_AUTHOR("Jerry Jacobs <jerry.jacobs@prodrive-technologies.com>");
87MODULE_AUTHOR("Aurelien Jacquiot <a-jacquiot@ti.com>");
88MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
89MODULE_AUTHOR("Andre van Herk <andre.van.herk@prodrive-technologies.com>");
90MODULE_DESCRIPTION("RapidIO mport character device driver");
91MODULE_LICENSE("GPL");
92MODULE_VERSION(DRV_VERSION);
93
94static int dma_timeout = 3000; /* DMA transfer timeout in msec */
95module_param(dma_timeout, int, S_IRUGO);
96MODULE_PARM_DESC(dma_timeout, "DMA Transfer Timeout in msec (default: 3000)");
97
98#ifdef DEBUG
99static u32 dbg_level = DBG_NONE;
100module_param(dbg_level, uint, S_IWUSR | S_IWGRP | S_IRUGO);
101MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
102#endif
103
104/*
105 * An internal DMA coherent buffer
106 */
107struct 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 */
119enum rio_mport_map_dir {
120 MAP_INBOUND,
121 MAP_OUTBOUND,
122 MAP_DMA,
123};
124
125struct rio_mport_mapping {
126 struct list_head node;
127 struct mport_dev *md;
128 enum rio_mport_map_dir dir;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700129 u16 rioid;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700130 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
138struct rio_mport_dma_map {
139 int valid;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700140 u64 length;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700141 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 */
166struct 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 */
202struct 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;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700211 u32 event_mask; /* RIO_DOORBELL, RIO_PORTWRITE */
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700212#ifdef CONFIG_RAPIDIO_DMA_ENGINE
213 struct dma_chan *dmach;
214 struct list_head async_list;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700215 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 */
229struct 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 */
243struct 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
250static LIST_HEAD(mport_devs);
251static DEFINE_MUTEX(mport_devs_lock);
252
253#if (0) /* used by commented out portion of poll function : FIXME */
254static DECLARE_WAIT_QUEUE_HEAD(mport_cdev_wait);
255#endif
256
257static struct class *dev_class;
258static dev_t dev_number;
259
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700260static void mport_release_mapping(struct kref *ref);
261
262static 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) ||
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700276 (maint_io.length == 0) || (maint_io.length % 4) ||
277 (maint_io.length + maint_io.offset) > RIO_MAINT_SPACE_SZ)
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700278 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
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700299 if (unlikely(copy_to_user((void __user *)(uintptr_t)maint_io.buffer,
300 buffer, maint_io.length)))
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700301 ret = -EFAULT;
302out:
303 vfree(buffer);
304 return ret;
305}
306
307static 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) ||
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700321 (maint_io.length == 0) || (maint_io.length % 4) ||
322 (maint_io.length + maint_io.offset) > RIO_MAINT_SPACE_SZ)
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700323 return -EINVAL;
324
325 buffer = vmalloc(maint_io.length);
326 if (buffer == NULL)
327 return -ENOMEM;
328 length = maint_io.length;
329
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700330 if (unlikely(copy_from_user(buffer,
331 (void __user *)(uintptr_t)maint_io.buffer, length))) {
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700332 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
353out:
354 vfree(buffer);
355 return ret;
356}
357
358
359/*
360 * Inbound/outbound memory mapping functions
361 */
362static int
363rio_mport_create_outbound_mapping(struct mport_dev *md, struct file *filp,
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700364 u16 rioid, u64 raddr, u32 size,
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700365 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
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700373 map = kzalloc(sizeof(*map), GFP_KERNEL);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700374 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;
391err_map_outb:
392 kfree(map);
393 return ret;
394}
395
396static int
397rio_mport_get_outbound_mapping(struct mport_dev *md, struct file *filp,
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700398 u16 rioid, u64 raddr, u32 size,
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700399 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
429static 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
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700437 if (unlikely(copy_from_user(&map, arg, sizeof(map))))
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700438 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
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700452 if (unlikely(copy_to_user(arg, &map, sizeof(map))))
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700453 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 */
463static 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
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700473 if (copy_from_user(&handle, arg, sizeof(handle)))
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700474 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 */
499static int maint_hdid_set(struct mport_cdev_priv *priv, void __user *arg)
500{
501 struct mport_dev *md = priv->md;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700502 u16 hdid;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700503
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700504 if (copy_from_user(&hdid, arg, sizeof(hdid)))
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700505 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 */
521static int maint_comptag_set(struct mport_cdev_priv *priv, void __user *arg)
522{
523 struct mport_dev *md = priv->md;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700524 u32 comptag;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700525
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700526 if (copy_from_user(&comptag, arg, sizeof(comptag)))
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700527 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
538struct mport_dma_req {
Ioan Nicubbd876a2018-04-10 16:35:10 -0700539 struct kref refcount;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700540 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
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700555static 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
565static 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
Ioan Nicubbd876a2018-04-10 16:35:10 -0700574static void dma_req_free(struct kref *ref)
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700575{
Ioan Nicubbd876a2018-04-10 16:35:10 -0700576 struct mport_dma_req *req = container_of(ref, struct mport_dma_req,
577 refcount);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700578 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
601static 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);
Ioan Nicubbd876a2018-04-10 16:35:10 -0700609 kref_put(&req->refcount, dma_req_free);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700610}
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 */
619static 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 */
657static 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
689static 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 */
697static 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
Ioan Nicubbd876a2018-04-10 16:35:10 -0700740 tx->callback = dma_xfer_callback;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700741 tx->callback_param = req;
742
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700743 req->status = DMA_IN_PROGRESS;
Ioan Nicubbd876a2018-04-10 16:35:10 -0700744 kref_get(&req->refcount);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700745
746 cookie = dmaengine_submit(tx);
747 req->cookie = cookie;
748
749 rmcd_debug(DMA, "pid=%d DMA_%s tx_cookie = %d", task_pid_nr(current),
750 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie);
751
752 if (dma_submit_error(cookie)) {
753 rmcd_error("submit err=%d (addr:0x%llx len:0x%llx)",
754 cookie, xfer->rio_addr, xfer->length);
Ioan Nicubbd876a2018-04-10 16:35:10 -0700755 kref_put(&req->refcount, dma_req_free);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700756 ret = -EIO;
757 goto err_out;
758 }
759
760 dma_async_issue_pending(chan);
761
762 if (sync == RIO_TRANSFER_ASYNC) {
763 spin_lock(&priv->req_lock);
764 list_add_tail(&req->node, &priv->async_list);
765 spin_unlock(&priv->req_lock);
766 return cookie;
767 } else if (sync == RIO_TRANSFER_FAF)
768 return 0;
769
770 wret = wait_for_completion_interruptible_timeout(&req->req_comp, tmo);
771
772 if (wret == 0) {
773 /* Timeout on wait occurred */
774 rmcd_error("%s(%d) timed out waiting for DMA_%s %d",
775 current->comm, task_pid_nr(current),
776 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie);
777 return -ETIMEDOUT;
778 } else if (wret == -ERESTARTSYS) {
779 /* Wait_for_completion was interrupted by a signal but DMA may
780 * be in progress
781 */
782 rmcd_error("%s(%d) wait for DMA_%s %d was interrupted",
783 current->comm, task_pid_nr(current),
784 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie);
785 return -EINTR;
786 }
787
788 if (req->status != DMA_COMPLETE) {
789 /* DMA transaction completion was signaled with error */
790 rmcd_error("%s(%d) DMA_%s %d completed with status %d (ret=%d)",
791 current->comm, task_pid_nr(current),
792 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE",
793 cookie, req->status, ret);
794 ret = -EIO;
795 }
796
797err_out:
798 return ret;
799}
800
801/*
802 * rio_dma_transfer() - Perform RapidIO DMA data transfer to/from
803 * the remote RapidIO device
804 * @filp: file pointer associated with the call
805 * @transfer_mode: DMA transfer mode
806 * @sync: synchronization mode
807 * @dir: DMA transfer direction (DMA_MEM_TO_DEV = write OR
808 * DMA_DEV_TO_MEM = read)
809 * @xfer: data transfer descriptor structure
810 */
811static int
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700812rio_dma_transfer(struct file *filp, u32 transfer_mode,
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700813 enum rio_transfer_sync sync, enum dma_data_direction dir,
814 struct rio_transfer_io *xfer)
815{
816 struct mport_cdev_priv *priv = filp->private_data;
817 unsigned long nr_pages = 0;
818 struct page **page_list = NULL;
819 struct mport_dma_req *req;
820 struct mport_dev *md = priv->md;
821 struct dma_chan *chan;
822 int i, ret;
823 int nents;
824
825 if (xfer->length == 0)
826 return -EINVAL;
827 req = kzalloc(sizeof(*req), GFP_KERNEL);
828 if (!req)
829 return -ENOMEM;
830
831 ret = get_dma_channel(priv);
832 if (ret) {
833 kfree(req);
834 return ret;
835 }
Ioan Nicuc5157b72018-04-20 14:55:49 -0700836 chan = priv->dmach;
837
838 kref_init(&req->refcount);
839 init_completion(&req->req_comp);
840 req->dir = dir;
841 req->filp = filp;
842 req->priv = priv;
843 req->dmach = chan;
844 req->sync = sync;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700845
846 /*
847 * If parameter loc_addr != NULL, we are transferring data from/to
848 * data buffer allocated in user-space: lock in memory user-space
849 * buffer pages and build an SG table for DMA transfer request
850 *
851 * Otherwise (loc_addr == NULL) contiguous kernel-space buffer is
852 * used for DMA data transfers: build single entry SG table using
853 * offset within the internal buffer specified by handle parameter.
854 */
855 if (xfer->loc_addr) {
Tvrtko Ursulinc4860ad2017-07-31 19:55:08 +0100856 unsigned int offset;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700857 long pinned;
858
Tvrtko Ursulinc4860ad2017-07-31 19:55:08 +0100859 offset = lower_32_bits(offset_in_page(xfer->loc_addr));
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700860 nr_pages = PAGE_ALIGN(xfer->length + offset) >> PAGE_SHIFT;
861
862 page_list = kmalloc_array(nr_pages,
863 sizeof(*page_list), GFP_KERNEL);
864 if (page_list == NULL) {
865 ret = -ENOMEM;
866 goto err_req;
867 }
868
Al Viro0ca36a62017-01-29 03:12:00 -0500869 pinned = get_user_pages_fast(
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700870 (unsigned long)xfer->loc_addr & PAGE_MASK,
Al Viro0ca36a62017-01-29 03:12:00 -0500871 nr_pages, dir == DMA_FROM_DEVICE, page_list);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700872
873 if (pinned != nr_pages) {
874 if (pinned < 0) {
Lorenzo Stoakes369f2672017-02-27 14:27:34 -0800875 rmcd_error("get_user_pages_unlocked err=%ld",
876 pinned);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700877 nr_pages = 0;
878 } else
879 rmcd_error("pinned %ld out of %ld pages",
880 pinned, nr_pages);
881 ret = -EFAULT;
882 goto err_pg;
883 }
884
885 ret = sg_alloc_table_from_pages(&req->sgt, page_list, nr_pages,
886 offset, xfer->length, GFP_KERNEL);
887 if (ret) {
888 rmcd_error("sg_alloc_table failed with err=%d", ret);
889 goto err_pg;
890 }
891
892 req->page_list = page_list;
893 req->nr_pages = nr_pages;
894 } else {
895 dma_addr_t baddr;
896 struct rio_mport_mapping *map;
897
898 baddr = (dma_addr_t)xfer->handle;
899
900 mutex_lock(&md->buf_mutex);
901 list_for_each_entry(map, &md->mappings, node) {
902 if (baddr >= map->phys_addr &&
903 baddr < (map->phys_addr + map->size)) {
904 kref_get(&map->ref);
905 req->map = map;
906 break;
907 }
908 }
909 mutex_unlock(&md->buf_mutex);
910
911 if (req->map == NULL) {
912 ret = -ENOMEM;
913 goto err_req;
914 }
915
916 if (xfer->length + xfer->offset > map->size) {
917 ret = -EINVAL;
918 goto err_req;
919 }
920
921 ret = sg_alloc_table(&req->sgt, 1, GFP_KERNEL);
922 if (unlikely(ret)) {
923 rmcd_error("sg_alloc_table failed for internal buf");
924 goto err_req;
925 }
926
927 sg_set_buf(req->sgt.sgl,
928 map->virt_addr + (baddr - map->phys_addr) +
929 xfer->offset, xfer->length);
930 }
931
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700932 nents = dma_map_sg(chan->device->dev,
933 req->sgt.sgl, req->sgt.nents, dir);
Christophe JAILLETc46d90c2017-11-17 15:38:03 -0800934 if (nents == 0) {
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700935 rmcd_error("Failed to map SG list");
Christophe JAILLETb1402dc2017-11-17 15:37:57 -0800936 ret = -EFAULT;
937 goto err_pg;
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700938 }
939
940 ret = do_dma_request(req, xfer, sync, nents);
941
942 if (ret >= 0) {
Ioan Nicubbd876a2018-04-10 16:35:10 -0700943 if (sync == RIO_TRANSFER_ASYNC)
944 return ret; /* return ASYNC cookie */
945 } else {
946 rmcd_debug(DMA, "do_dma_request failed with err=%d", ret);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700947 }
948
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700949err_pg:
Ioan Nicubbd876a2018-04-10 16:35:10 -0700950 if (!req->page_list) {
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700951 for (i = 0; i < nr_pages; i++)
952 put_page(page_list[i]);
953 kfree(page_list);
954 }
955err_req:
Ioan Nicubbd876a2018-04-10 16:35:10 -0700956 kref_put(&req->refcount, dma_req_free);
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700957 return ret;
958}
959
960static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg)
961{
962 struct mport_cdev_priv *priv = filp->private_data;
963 struct rio_transaction transaction;
964 struct rio_transfer_io *transfer;
965 enum dma_data_direction dir;
966 int i, ret = 0;
967
968 if (unlikely(copy_from_user(&transaction, arg, sizeof(transaction))))
969 return -EFAULT;
970
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700971 if (transaction.count != 1) /* only single transfer for now */
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700972 return -EINVAL;
973
974 if ((transaction.transfer_mode &
975 priv->md->properties.transfer_mode) == 0)
976 return -ENODEV;
977
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700978 transfer = vmalloc(transaction.count * sizeof(*transfer));
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700979 if (!transfer)
980 return -ENOMEM;
981
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700982 if (unlikely(copy_from_user(transfer,
983 (void __user *)(uintptr_t)transaction.block,
984 transaction.count * sizeof(*transfer)))) {
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700985 ret = -EFAULT;
986 goto out_free;
987 }
988
989 dir = (transaction.dir == RIO_TRANSFER_DIR_READ) ?
990 DMA_FROM_DEVICE : DMA_TO_DEVICE;
991 for (i = 0; i < transaction.count && ret == 0; i++)
992 ret = rio_dma_transfer(filp, transaction.transfer_mode,
993 transaction.sync, dir, &transfer[i]);
994
Alexandre Bounine4e1016d2016-05-05 16:22:06 -0700995 if (unlikely(copy_to_user((void __user *)(uintptr_t)transaction.block,
996 transfer,
997 transaction.count * sizeof(*transfer))))
Alexandre Bouninee8de3702016-03-22 14:27:08 -0700998 ret = -EFAULT;
999
1000out_free:
1001 vfree(transfer);
1002
1003 return ret;
1004}
1005
1006static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg)
1007{
1008 struct mport_cdev_priv *priv;
1009 struct mport_dev *md;
1010 struct rio_async_tx_wait w_param;
1011 struct mport_dma_req *req;
1012 dma_cookie_t cookie;
1013 unsigned long tmo;
1014 long wret;
1015 int found = 0;
1016 int ret;
1017
1018 priv = (struct mport_cdev_priv *)filp->private_data;
1019 md = priv->md;
1020
1021 if (unlikely(copy_from_user(&w_param, arg, sizeof(w_param))))
1022 return -EFAULT;
1023
1024 cookie = w_param.token;
1025 if (w_param.timeout)
1026 tmo = msecs_to_jiffies(w_param.timeout);
1027 else /* Use default DMA timeout */
1028 tmo = msecs_to_jiffies(dma_timeout);
1029
1030 spin_lock(&priv->req_lock);
1031 list_for_each_entry(req, &priv->async_list, node) {
1032 if (req->cookie == cookie) {
1033 list_del(&req->node);
1034 found = 1;
1035 break;
1036 }
1037 }
1038 spin_unlock(&priv->req_lock);
1039
1040 if (!found)
1041 return -EAGAIN;
1042
1043 wret = wait_for_completion_interruptible_timeout(&req->req_comp, tmo);
1044
1045 if (wret == 0) {
1046 /* Timeout on wait occurred */
1047 rmcd_error("%s(%d) timed out waiting for ASYNC DMA_%s",
1048 current->comm, task_pid_nr(current),
1049 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE");
1050 ret = -ETIMEDOUT;
1051 goto err_tmo;
1052 } else if (wret == -ERESTARTSYS) {
1053 /* Wait_for_completion was interrupted by a signal but DMA may
1054 * be still in progress
1055 */
1056 rmcd_error("%s(%d) wait for ASYNC DMA_%s was interrupted",
1057 current->comm, task_pid_nr(current),
1058 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE");
1059 ret = -EINTR;
1060 goto err_tmo;
1061 }
1062
1063 if (req->status != DMA_COMPLETE) {
1064 /* DMA transaction completion signaled with transfer error */
1065 rmcd_error("%s(%d) ASYNC DMA_%s completion with status %d",
1066 current->comm, task_pid_nr(current),
1067 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE",
1068 req->status);
1069 ret = -EIO;
1070 } else
1071 ret = 0;
1072
1073 if (req->status != DMA_IN_PROGRESS && req->status != DMA_PAUSED)
Ioan Nicubbd876a2018-04-10 16:35:10 -07001074 kref_put(&req->refcount, dma_req_free);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001075
1076 return ret;
1077
1078err_tmo:
1079 /* Return request back into async queue */
1080 spin_lock(&priv->req_lock);
1081 list_add_tail(&req->node, &priv->async_list);
1082 spin_unlock(&priv->req_lock);
1083 return ret;
1084}
1085
1086static int rio_mport_create_dma_mapping(struct mport_dev *md, struct file *filp,
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001087 u64 size, struct rio_mport_mapping **mapping)
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001088{
1089 struct rio_mport_mapping *map;
1090
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001091 map = kzalloc(sizeof(*map), GFP_KERNEL);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001092 if (map == NULL)
1093 return -ENOMEM;
1094
1095 map->virt_addr = dma_alloc_coherent(md->mport->dev.parent, size,
1096 &map->phys_addr, GFP_KERNEL);
1097 if (map->virt_addr == NULL) {
1098 kfree(map);
1099 return -ENOMEM;
1100 }
1101
1102 map->dir = MAP_DMA;
1103 map->size = size;
1104 map->filp = filp;
1105 map->md = md;
1106 kref_init(&map->ref);
1107 mutex_lock(&md->buf_mutex);
1108 list_add_tail(&map->node, &md->mappings);
1109 mutex_unlock(&md->buf_mutex);
1110 *mapping = map;
1111
1112 return 0;
1113}
1114
1115static int rio_mport_alloc_dma(struct file *filp, void __user *arg)
1116{
1117 struct mport_cdev_priv *priv = filp->private_data;
1118 struct mport_dev *md = priv->md;
1119 struct rio_dma_mem map;
1120 struct rio_mport_mapping *mapping = NULL;
1121 int ret;
1122
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001123 if (unlikely(copy_from_user(&map, arg, sizeof(map))))
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001124 return -EFAULT;
1125
1126 ret = rio_mport_create_dma_mapping(md, filp, map.length, &mapping);
1127 if (ret)
1128 return ret;
1129
1130 map.dma_handle = mapping->phys_addr;
1131
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001132 if (unlikely(copy_to_user(arg, &map, sizeof(map)))) {
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001133 mutex_lock(&md->buf_mutex);
1134 kref_put(&mapping->ref, mport_release_mapping);
1135 mutex_unlock(&md->buf_mutex);
1136 return -EFAULT;
1137 }
1138
1139 return 0;
1140}
1141
1142static int rio_mport_free_dma(struct file *filp, void __user *arg)
1143{
1144 struct mport_cdev_priv *priv = filp->private_data;
1145 struct mport_dev *md = priv->md;
1146 u64 handle;
1147 int ret = -EFAULT;
1148 struct rio_mport_mapping *map, *_map;
1149
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001150 if (copy_from_user(&handle, arg, sizeof(handle)))
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001151 return -EFAULT;
1152 rmcd_debug(EXIT, "filp=%p", filp);
1153
1154 mutex_lock(&md->buf_mutex);
1155 list_for_each_entry_safe(map, _map, &md->mappings, node) {
1156 if (map->dir == MAP_DMA && map->phys_addr == handle &&
1157 map->filp == filp) {
1158 kref_put(&map->ref, mport_release_mapping);
1159 ret = 0;
1160 break;
1161 }
1162 }
1163 mutex_unlock(&md->buf_mutex);
1164
1165 if (ret == -EFAULT) {
1166 rmcd_debug(DMA, "ERR no matching mapping");
1167 return ret;
1168 }
1169
1170 return 0;
1171}
1172#else
1173static int rio_mport_transfer_ioctl(struct file *filp, void *arg)
1174{
1175 return -ENODEV;
1176}
1177
1178static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg)
1179{
1180 return -ENODEV;
1181}
1182
1183static int rio_mport_alloc_dma(struct file *filp, void __user *arg)
1184{
1185 return -ENODEV;
1186}
1187
1188static int rio_mport_free_dma(struct file *filp, void __user *arg)
1189{
1190 return -ENODEV;
1191}
1192#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1193
1194/*
1195 * Inbound/outbound memory mapping functions
1196 */
1197
1198static int
1199rio_mport_create_inbound_mapping(struct mport_dev *md, struct file *filp,
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001200 u64 raddr, u64 size,
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001201 struct rio_mport_mapping **mapping)
1202{
1203 struct rio_mport *mport = md->mport;
1204 struct rio_mport_mapping *map;
1205 int ret;
1206
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001207 /* rio_map_inb_region() accepts u32 size */
1208 if (size > 0xffffffff)
1209 return -EINVAL;
1210
1211 map = kzalloc(sizeof(*map), GFP_KERNEL);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001212 if (map == NULL)
1213 return -ENOMEM;
1214
1215 map->virt_addr = dma_alloc_coherent(mport->dev.parent, size,
1216 &map->phys_addr, GFP_KERNEL);
1217 if (map->virt_addr == NULL) {
1218 ret = -ENOMEM;
1219 goto err_dma_alloc;
1220 }
1221
1222 if (raddr == RIO_MAP_ANY_ADDR)
1223 raddr = map->phys_addr;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001224 ret = rio_map_inb_region(mport, map->phys_addr, raddr, (u32)size, 0);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001225 if (ret < 0)
1226 goto err_map_inb;
1227
1228 map->dir = MAP_INBOUND;
1229 map->rio_addr = raddr;
1230 map->size = size;
1231 map->filp = filp;
1232 map->md = md;
1233 kref_init(&map->ref);
1234 mutex_lock(&md->buf_mutex);
1235 list_add_tail(&map->node, &md->mappings);
1236 mutex_unlock(&md->buf_mutex);
1237 *mapping = map;
1238 return 0;
1239
1240err_map_inb:
1241 dma_free_coherent(mport->dev.parent, size,
1242 map->virt_addr, map->phys_addr);
1243err_dma_alloc:
1244 kfree(map);
1245 return ret;
1246}
1247
1248static int
1249rio_mport_get_inbound_mapping(struct mport_dev *md, struct file *filp,
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001250 u64 raddr, u64 size,
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001251 struct rio_mport_mapping **mapping)
1252{
1253 struct rio_mport_mapping *map;
1254 int err = -ENOMEM;
1255
1256 if (raddr == RIO_MAP_ANY_ADDR)
1257 goto get_new;
1258
1259 mutex_lock(&md->buf_mutex);
1260 list_for_each_entry(map, &md->mappings, node) {
1261 if (map->dir != MAP_INBOUND)
1262 continue;
1263 if (raddr == map->rio_addr && size == map->size) {
1264 /* allow exact match only */
1265 *mapping = map;
1266 err = 0;
1267 break;
1268 } else if (raddr < (map->rio_addr + map->size - 1) &&
1269 (raddr + size) > map->rio_addr) {
1270 err = -EBUSY;
1271 break;
1272 }
1273 }
1274 mutex_unlock(&md->buf_mutex);
1275
1276 if (err != -ENOMEM)
1277 return err;
1278get_new:
1279 /* not found, create new */
1280 return rio_mport_create_inbound_mapping(md, filp, raddr, size, mapping);
1281}
1282
1283static int rio_mport_map_inbound(struct file *filp, void __user *arg)
1284{
1285 struct mport_cdev_priv *priv = filp->private_data;
1286 struct mport_dev *md = priv->md;
1287 struct rio_mmap map;
1288 struct rio_mport_mapping *mapping = NULL;
1289 int ret;
1290
1291 if (!md->mport->ops->map_inb)
1292 return -EPROTONOSUPPORT;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001293 if (unlikely(copy_from_user(&map, arg, sizeof(map))))
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001294 return -EFAULT;
1295
1296 rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp);
1297
1298 ret = rio_mport_get_inbound_mapping(md, filp, map.rio_addr,
1299 map.length, &mapping);
1300 if (ret)
1301 return ret;
1302
1303 map.handle = mapping->phys_addr;
1304 map.rio_addr = mapping->rio_addr;
1305
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001306 if (unlikely(copy_to_user(arg, &map, sizeof(map)))) {
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001307 /* Delete mapping if it was created by this request */
1308 if (ret == 0 && mapping->filp == filp) {
1309 mutex_lock(&md->buf_mutex);
1310 kref_put(&mapping->ref, mport_release_mapping);
1311 mutex_unlock(&md->buf_mutex);
1312 }
1313 return -EFAULT;
1314 }
1315
1316 return 0;
1317}
1318
1319/*
1320 * rio_mport_inbound_free() - unmap from RapidIO address space and free
1321 * previously allocated inbound DMA coherent buffer
1322 * @priv: driver private data
1323 * @arg: buffer handle returned by allocation routine
1324 */
1325static int rio_mport_inbound_free(struct file *filp, void __user *arg)
1326{
1327 struct mport_cdev_priv *priv = filp->private_data;
1328 struct mport_dev *md = priv->md;
1329 u64 handle;
1330 struct rio_mport_mapping *map, *_map;
1331
1332 rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp);
1333
1334 if (!md->mport->ops->unmap_inb)
1335 return -EPROTONOSUPPORT;
1336
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001337 if (copy_from_user(&handle, arg, sizeof(handle)))
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001338 return -EFAULT;
1339
1340 mutex_lock(&md->buf_mutex);
1341 list_for_each_entry_safe(map, _map, &md->mappings, node) {
1342 if (map->dir == MAP_INBOUND && map->phys_addr == handle) {
1343 if (map->filp == filp) {
1344 map->filp = NULL;
1345 kref_put(&map->ref, mport_release_mapping);
1346 }
1347 break;
1348 }
1349 }
1350 mutex_unlock(&md->buf_mutex);
1351
1352 return 0;
1353}
1354
1355/*
1356 * maint_port_idx_get() - Get the port index of the mport instance
1357 * @priv: driver private data
1358 * @arg: port index
1359 */
1360static int maint_port_idx_get(struct mport_cdev_priv *priv, void __user *arg)
1361{
1362 struct mport_dev *md = priv->md;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001363 u32 port_idx = md->mport->index;
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001364
1365 rmcd_debug(MPORT, "port_index=%d", port_idx);
1366
1367 if (copy_to_user(arg, &port_idx, sizeof(port_idx)))
1368 return -EFAULT;
1369
1370 return 0;
1371}
1372
1373static int rio_mport_add_event(struct mport_cdev_priv *priv,
1374 struct rio_event *event)
1375{
1376 int overflow;
1377
1378 if (!(priv->event_mask & event->header))
1379 return -EACCES;
1380
1381 spin_lock(&priv->fifo_lock);
1382 overflow = kfifo_avail(&priv->event_fifo) < sizeof(*event)
1383 || kfifo_in(&priv->event_fifo, (unsigned char *)event,
1384 sizeof(*event)) != sizeof(*event);
1385 spin_unlock(&priv->fifo_lock);
1386
1387 wake_up_interruptible(&priv->event_rx_wait);
1388
1389 if (overflow) {
1390 dev_warn(&priv->md->dev, DRV_NAME ": event fifo overflow\n");
1391 return -EBUSY;
1392 }
1393
1394 return 0;
1395}
1396
1397static void rio_mport_doorbell_handler(struct rio_mport *mport, void *dev_id,
1398 u16 src, u16 dst, u16 info)
1399{
1400 struct mport_dev *data = dev_id;
1401 struct mport_cdev_priv *priv;
1402 struct rio_mport_db_filter *db_filter;
1403 struct rio_event event;
1404 int handled;
1405
1406 event.header = RIO_DOORBELL;
1407 event.u.doorbell.rioid = src;
1408 event.u.doorbell.payload = info;
1409
1410 handled = 0;
1411 spin_lock(&data->db_lock);
1412 list_for_each_entry(db_filter, &data->doorbells, data_node) {
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001413 if (((db_filter->filter.rioid == RIO_INVALID_DESTID ||
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001414 db_filter->filter.rioid == src)) &&
1415 info >= db_filter->filter.low &&
1416 info <= db_filter->filter.high) {
1417 priv = db_filter->priv;
1418 rio_mport_add_event(priv, &event);
1419 handled = 1;
1420 }
1421 }
1422 spin_unlock(&data->db_lock);
1423
1424 if (!handled)
1425 dev_warn(&data->dev,
1426 "%s: spurious DB received from 0x%x, info=0x%04x\n",
1427 __func__, src, info);
1428}
1429
1430static int rio_mport_add_db_filter(struct mport_cdev_priv *priv,
1431 void __user *arg)
1432{
1433 struct mport_dev *md = priv->md;
1434 struct rio_mport_db_filter *db_filter;
1435 struct rio_doorbell_filter filter;
1436 unsigned long flags;
1437 int ret;
1438
1439 if (copy_from_user(&filter, arg, sizeof(filter)))
1440 return -EFAULT;
1441
1442 if (filter.low > filter.high)
1443 return -EINVAL;
1444
1445 ret = rio_request_inb_dbell(md->mport, md, filter.low, filter.high,
1446 rio_mport_doorbell_handler);
1447 if (ret) {
1448 rmcd_error("%s failed to register IBDB, err=%d",
1449 dev_name(&md->dev), ret);
1450 return ret;
1451 }
1452
1453 db_filter = kzalloc(sizeof(*db_filter), GFP_KERNEL);
1454 if (db_filter == NULL) {
1455 rio_release_inb_dbell(md->mport, filter.low, filter.high);
1456 return -ENOMEM;
1457 }
1458
1459 db_filter->filter = filter;
1460 db_filter->priv = priv;
1461 spin_lock_irqsave(&md->db_lock, flags);
1462 list_add_tail(&db_filter->priv_node, &priv->db_filters);
1463 list_add_tail(&db_filter->data_node, &md->doorbells);
1464 spin_unlock_irqrestore(&md->db_lock, flags);
1465
1466 return 0;
1467}
1468
1469static void rio_mport_delete_db_filter(struct rio_mport_db_filter *db_filter)
1470{
1471 list_del(&db_filter->data_node);
1472 list_del(&db_filter->priv_node);
1473 kfree(db_filter);
1474}
1475
1476static int rio_mport_remove_db_filter(struct mport_cdev_priv *priv,
1477 void __user *arg)
1478{
1479 struct rio_mport_db_filter *db_filter;
1480 struct rio_doorbell_filter filter;
1481 unsigned long flags;
1482 int ret = -EINVAL;
1483
1484 if (copy_from_user(&filter, arg, sizeof(filter)))
1485 return -EFAULT;
1486
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001487 if (filter.low > filter.high)
1488 return -EINVAL;
1489
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001490 spin_lock_irqsave(&priv->md->db_lock, flags);
1491 list_for_each_entry(db_filter, &priv->db_filters, priv_node) {
1492 if (db_filter->filter.rioid == filter.rioid &&
1493 db_filter->filter.low == filter.low &&
1494 db_filter->filter.high == filter.high) {
1495 rio_mport_delete_db_filter(db_filter);
1496 ret = 0;
1497 break;
1498 }
1499 }
1500 spin_unlock_irqrestore(&priv->md->db_lock, flags);
1501
1502 if (!ret)
1503 rio_release_inb_dbell(priv->md->mport, filter.low, filter.high);
1504
1505 return ret;
1506}
1507
1508static int rio_mport_match_pw(union rio_pw_msg *msg,
1509 struct rio_pw_filter *filter)
1510{
1511 if ((msg->em.comptag & filter->mask) < filter->low ||
1512 (msg->em.comptag & filter->mask) > filter->high)
1513 return 0;
1514 return 1;
1515}
1516
1517static int rio_mport_pw_handler(struct rio_mport *mport, void *context,
1518 union rio_pw_msg *msg, int step)
1519{
1520 struct mport_dev *md = context;
1521 struct mport_cdev_priv *priv;
1522 struct rio_mport_pw_filter *pw_filter;
1523 struct rio_event event;
1524 int handled;
1525
1526 event.header = RIO_PORTWRITE;
1527 memcpy(event.u.portwrite.payload, msg->raw, RIO_PW_MSG_SIZE);
1528
1529 handled = 0;
1530 spin_lock(&md->pw_lock);
1531 list_for_each_entry(pw_filter, &md->portwrites, md_node) {
1532 if (rio_mport_match_pw(msg, &pw_filter->filter)) {
1533 priv = pw_filter->priv;
1534 rio_mport_add_event(priv, &event);
1535 handled = 1;
1536 }
1537 }
1538 spin_unlock(&md->pw_lock);
1539
1540 if (!handled) {
1541 printk_ratelimited(KERN_WARNING DRV_NAME
1542 ": mport%d received spurious PW from 0x%08x\n",
1543 mport->id, msg->em.comptag);
1544 }
1545
1546 return 0;
1547}
1548
1549static int rio_mport_add_pw_filter(struct mport_cdev_priv *priv,
1550 void __user *arg)
1551{
1552 struct mport_dev *md = priv->md;
1553 struct rio_mport_pw_filter *pw_filter;
1554 struct rio_pw_filter filter;
1555 unsigned long flags;
1556 int hadd = 0;
1557
1558 if (copy_from_user(&filter, arg, sizeof(filter)))
1559 return -EFAULT;
1560
1561 pw_filter = kzalloc(sizeof(*pw_filter), GFP_KERNEL);
1562 if (pw_filter == NULL)
1563 return -ENOMEM;
1564
1565 pw_filter->filter = filter;
1566 pw_filter->priv = priv;
1567 spin_lock_irqsave(&md->pw_lock, flags);
1568 if (list_empty(&md->portwrites))
1569 hadd = 1;
1570 list_add_tail(&pw_filter->priv_node, &priv->pw_filters);
1571 list_add_tail(&pw_filter->md_node, &md->portwrites);
1572 spin_unlock_irqrestore(&md->pw_lock, flags);
1573
1574 if (hadd) {
1575 int ret;
1576
1577 ret = rio_add_mport_pw_handler(md->mport, md,
1578 rio_mport_pw_handler);
1579 if (ret) {
1580 dev_err(&md->dev,
1581 "%s: failed to add IB_PW handler, err=%d\n",
1582 __func__, ret);
1583 return ret;
1584 }
1585 rio_pw_enable(md->mport, 1);
1586 }
1587
1588 return 0;
1589}
1590
1591static void rio_mport_delete_pw_filter(struct rio_mport_pw_filter *pw_filter)
1592{
1593 list_del(&pw_filter->md_node);
1594 list_del(&pw_filter->priv_node);
1595 kfree(pw_filter);
1596}
1597
1598static int rio_mport_match_pw_filter(struct rio_pw_filter *a,
1599 struct rio_pw_filter *b)
1600{
1601 if ((a->mask == b->mask) && (a->low == b->low) && (a->high == b->high))
1602 return 1;
1603 return 0;
1604}
1605
1606static int rio_mport_remove_pw_filter(struct mport_cdev_priv *priv,
1607 void __user *arg)
1608{
1609 struct mport_dev *md = priv->md;
1610 struct rio_mport_pw_filter *pw_filter;
1611 struct rio_pw_filter filter;
1612 unsigned long flags;
1613 int ret = -EINVAL;
1614 int hdel = 0;
1615
1616 if (copy_from_user(&filter, arg, sizeof(filter)))
1617 return -EFAULT;
1618
1619 spin_lock_irqsave(&md->pw_lock, flags);
1620 list_for_each_entry(pw_filter, &priv->pw_filters, priv_node) {
1621 if (rio_mport_match_pw_filter(&pw_filter->filter, &filter)) {
1622 rio_mport_delete_pw_filter(pw_filter);
1623 ret = 0;
1624 break;
1625 }
1626 }
1627
1628 if (list_empty(&md->portwrites))
1629 hdel = 1;
1630 spin_unlock_irqrestore(&md->pw_lock, flags);
1631
1632 if (hdel) {
1633 rio_del_mport_pw_handler(md->mport, priv->md,
1634 rio_mport_pw_handler);
1635 rio_pw_enable(md->mport, 0);
1636 }
1637
1638 return ret;
1639}
1640
1641/*
1642 * rio_release_dev - release routine for kernel RIO device object
1643 * @dev: kernel device object associated with a RIO device structure
1644 *
1645 * Frees a RIO device struct associated a RIO device struct.
1646 * The RIO device struct is freed.
1647 */
1648static void rio_release_dev(struct device *dev)
1649{
1650 struct rio_dev *rdev;
1651
1652 rdev = to_rio_dev(dev);
1653 pr_info(DRV_PREFIX "%s: %s\n", __func__, rio_name(rdev));
1654 kfree(rdev);
1655}
1656
1657
1658static void rio_release_net(struct device *dev)
1659{
1660 struct rio_net *net;
1661
1662 net = to_rio_net(dev);
1663 rmcd_debug(RDEV, "net_%d", net->id);
1664 kfree(net);
1665}
1666
1667
1668/*
1669 * rio_mport_add_riodev - creates a kernel RIO device object
1670 *
1671 * Allocates a RIO device data structure and initializes required fields based
1672 * on device's configuration space contents.
1673 * If the device has switch capabilities, then a switch specific portion is
1674 * allocated and configured.
1675 */
1676static int rio_mport_add_riodev(struct mport_cdev_priv *priv,
1677 void __user *arg)
1678{
1679 struct mport_dev *md = priv->md;
1680 struct rio_rdev_info dev_info;
1681 struct rio_dev *rdev;
1682 struct rio_switch *rswitch = NULL;
1683 struct rio_mport *mport;
1684 size_t size;
1685 u32 rval;
1686 u32 swpinfo = 0;
1687 u16 destid;
1688 u8 hopcount;
1689 int err;
1690
1691 if (copy_from_user(&dev_info, arg, sizeof(dev_info)))
1692 return -EFAULT;
1693
1694 rmcd_debug(RDEV, "name:%s ct:0x%x did:0x%x hc:0x%x", dev_info.name,
1695 dev_info.comptag, dev_info.destid, dev_info.hopcount);
1696
1697 if (bus_find_device_by_name(&rio_bus_type, NULL, dev_info.name)) {
1698 rmcd_debug(RDEV, "device %s already exists", dev_info.name);
1699 return -EEXIST;
1700 }
1701
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001702 size = sizeof(*rdev);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001703 mport = md->mport;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001704 destid = dev_info.destid;
1705 hopcount = dev_info.hopcount;
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001706
1707 if (rio_mport_read_config_32(mport, destid, hopcount,
1708 RIO_PEF_CAR, &rval))
1709 return -EIO;
1710
1711 if (rval & RIO_PEF_SWITCH) {
1712 rio_mport_read_config_32(mport, destid, hopcount,
1713 RIO_SWP_INFO_CAR, &swpinfo);
1714 size += (RIO_GET_TOTAL_PORTS(swpinfo) *
1715 sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
1716 }
1717
1718 rdev = kzalloc(size, GFP_KERNEL);
1719 if (rdev == NULL)
1720 return -ENOMEM;
1721
1722 if (mport->net == NULL) {
1723 struct rio_net *net;
1724
1725 net = rio_alloc_net(mport);
1726 if (!net) {
1727 err = -ENOMEM;
1728 rmcd_debug(RDEV, "failed to allocate net object");
1729 goto cleanup;
1730 }
1731
1732 net->id = mport->id;
1733 net->hport = mport;
1734 dev_set_name(&net->dev, "rnet_%d", net->id);
1735 net->dev.parent = &mport->dev;
1736 net->dev.release = rio_release_net;
1737 err = rio_add_net(net);
1738 if (err) {
1739 rmcd_debug(RDEV, "failed to register net, err=%d", err);
1740 kfree(net);
1741 goto cleanup;
1742 }
1743 }
1744
1745 rdev->net = mport->net;
1746 rdev->pef = rval;
1747 rdev->swpinfo = swpinfo;
1748 rio_mport_read_config_32(mport, destid, hopcount,
1749 RIO_DEV_ID_CAR, &rval);
1750 rdev->did = rval >> 16;
1751 rdev->vid = rval & 0xffff;
1752 rio_mport_read_config_32(mport, destid, hopcount, RIO_DEV_INFO_CAR,
1753 &rdev->device_rev);
1754 rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_ID_CAR,
1755 &rval);
1756 rdev->asm_did = rval >> 16;
1757 rdev->asm_vid = rval & 0xffff;
1758 rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_INFO_CAR,
1759 &rval);
1760 rdev->asm_rev = rval >> 16;
1761
1762 if (rdev->pef & RIO_PEF_EXT_FEATURES) {
1763 rdev->efptr = rval & 0xffff;
1764 rdev->phys_efptr = rio_mport_get_physefb(mport, 0, destid,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001765 hopcount, &rdev->phys_rmap);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001766
1767 rdev->em_efptr = rio_mport_get_feature(mport, 0, destid,
1768 hopcount, RIO_EFB_ERR_MGMNT);
1769 }
1770
1771 rio_mport_read_config_32(mport, destid, hopcount, RIO_SRC_OPS_CAR,
1772 &rdev->src_ops);
1773 rio_mport_read_config_32(mport, destid, hopcount, RIO_DST_OPS_CAR,
1774 &rdev->dst_ops);
1775
1776 rdev->comp_tag = dev_info.comptag;
1777 rdev->destid = destid;
1778 /* hopcount is stored as specified by a caller, regardles of EP or SW */
1779 rdev->hopcount = hopcount;
1780
1781 if (rdev->pef & RIO_PEF_SWITCH) {
1782 rswitch = rdev->rswitch;
1783 rswitch->route_table = NULL;
1784 }
1785
1786 if (strlen(dev_info.name))
1787 dev_set_name(&rdev->dev, "%s", dev_info.name);
1788 else if (rdev->pef & RIO_PEF_SWITCH)
1789 dev_set_name(&rdev->dev, "%02x:s:%04x", mport->id,
1790 rdev->comp_tag & RIO_CTAG_UDEVID);
1791 else
1792 dev_set_name(&rdev->dev, "%02x:e:%04x", mport->id,
1793 rdev->comp_tag & RIO_CTAG_UDEVID);
1794
1795 INIT_LIST_HEAD(&rdev->net_list);
1796 rdev->dev.parent = &mport->net->dev;
1797 rio_attach_device(rdev);
1798 rdev->dev.release = rio_release_dev;
1799
1800 if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
1801 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
1802 0, 0xffff);
1803 err = rio_add_device(rdev);
1804 if (err)
1805 goto cleanup;
1806 rio_dev_get(rdev);
1807
1808 return 0;
1809cleanup:
1810 kfree(rdev);
1811 return err;
1812}
1813
1814static int rio_mport_del_riodev(struct mport_cdev_priv *priv, void __user *arg)
1815{
1816 struct rio_rdev_info dev_info;
1817 struct rio_dev *rdev = NULL;
1818 struct device *dev;
1819 struct rio_mport *mport;
1820 struct rio_net *net;
1821
1822 if (copy_from_user(&dev_info, arg, sizeof(dev_info)))
1823 return -EFAULT;
1824
1825 mport = priv->md->mport;
1826
1827 /* If device name is specified, removal by name has priority */
1828 if (strlen(dev_info.name)) {
1829 dev = bus_find_device_by_name(&rio_bus_type, NULL,
1830 dev_info.name);
1831 if (dev)
1832 rdev = to_rio_dev(dev);
1833 } else {
1834 do {
1835 rdev = rio_get_comptag(dev_info.comptag, rdev);
1836 if (rdev && rdev->dev.parent == &mport->net->dev &&
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07001837 rdev->destid == dev_info.destid &&
1838 rdev->hopcount == dev_info.hopcount)
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001839 break;
1840 } while (rdev);
1841 }
1842
1843 if (!rdev) {
1844 rmcd_debug(RDEV,
1845 "device name:%s ct:0x%x did:0x%x hc:0x%x not found",
1846 dev_info.name, dev_info.comptag, dev_info.destid,
1847 dev_info.hopcount);
1848 return -ENODEV;
1849 }
1850
1851 net = rdev->net;
1852 rio_dev_put(rdev);
1853 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
1854
1855 if (list_empty(&net->devices)) {
1856 rio_free_net(net);
1857 mport->net = NULL;
1858 }
1859
1860 return 0;
1861}
1862
1863/*
1864 * Mport cdev management
1865 */
1866
1867/*
1868 * mport_cdev_open() - Open character device (mport)
1869 */
1870static int mport_cdev_open(struct inode *inode, struct file *filp)
1871{
1872 int ret;
1873 int minor = iminor(inode);
1874 struct mport_dev *chdev;
1875 struct mport_cdev_priv *priv;
1876
1877 /* Test for valid device */
1878 if (minor >= RIO_MAX_MPORTS) {
1879 rmcd_error("Invalid minor device number");
1880 return -EINVAL;
1881 }
1882
1883 chdev = container_of(inode->i_cdev, struct mport_dev, cdev);
1884
1885 rmcd_debug(INIT, "%s filp=%p", dev_name(&chdev->dev), filp);
1886
1887 if (atomic_read(&chdev->active) == 0)
1888 return -ENODEV;
1889
1890 get_device(&chdev->dev);
1891
1892 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1893 if (!priv) {
1894 put_device(&chdev->dev);
1895 return -ENOMEM;
1896 }
1897
1898 priv->md = chdev;
1899
1900 mutex_lock(&chdev->file_mutex);
1901 list_add_tail(&priv->list, &chdev->file_list);
1902 mutex_unlock(&chdev->file_mutex);
1903
1904 INIT_LIST_HEAD(&priv->db_filters);
1905 INIT_LIST_HEAD(&priv->pw_filters);
1906 spin_lock_init(&priv->fifo_lock);
1907 init_waitqueue_head(&priv->event_rx_wait);
1908 ret = kfifo_alloc(&priv->event_fifo,
1909 sizeof(struct rio_event) * MPORT_EVENT_DEPTH,
1910 GFP_KERNEL);
1911 if (ret < 0) {
1912 dev_err(&chdev->dev, DRV_NAME ": kfifo_alloc failed\n");
1913 ret = -ENOMEM;
1914 goto err_fifo;
1915 }
1916
1917#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1918 INIT_LIST_HEAD(&priv->async_list);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001919 spin_lock_init(&priv->req_lock);
1920 mutex_init(&priv->dma_lock);
1921#endif
1922
1923 filp->private_data = priv;
1924 goto out;
1925err_fifo:
1926 kfree(priv);
1927out:
1928 return ret;
1929}
1930
1931static int mport_cdev_fasync(int fd, struct file *filp, int mode)
1932{
1933 struct mport_cdev_priv *priv = filp->private_data;
1934
1935 return fasync_helper(fd, filp, mode, &priv->async_queue);
1936}
1937
1938#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1939static void mport_cdev_release_dma(struct file *filp)
1940{
1941 struct mport_cdev_priv *priv = filp->private_data;
1942 struct mport_dev *md;
1943 struct mport_dma_req *req, *req_next;
1944 unsigned long tmo = msecs_to_jiffies(dma_timeout);
1945 long wret;
1946 LIST_HEAD(list);
1947
1948 rmcd_debug(EXIT, "from filp=%p %s(%d)",
1949 filp, current->comm, task_pid_nr(current));
1950
1951 if (!priv->dmach) {
1952 rmcd_debug(EXIT, "No DMA channel for filp=%p", filp);
1953 return;
1954 }
1955
1956 md = priv->md;
1957
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001958 spin_lock(&priv->req_lock);
1959 if (!list_empty(&priv->async_list)) {
1960 rmcd_debug(EXIT, "async list not empty filp=%p %s(%d)",
1961 filp, current->comm, task_pid_nr(current));
1962 list_splice_init(&priv->async_list, &list);
1963 }
1964 spin_unlock(&priv->req_lock);
1965
1966 if (!list_empty(&list)) {
1967 rmcd_debug(EXIT, "temp list not empty");
1968 list_for_each_entry_safe(req, req_next, &list, node) {
1969 rmcd_debug(EXIT, "free req->filp=%p cookie=%d compl=%s",
1970 req->filp, req->cookie,
1971 completion_done(&req->req_comp)?"yes":"no");
1972 list_del(&req->node);
Ioan Nicubbd876a2018-04-10 16:35:10 -07001973 kref_put(&req->refcount, dma_req_free);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001974 }
1975 }
1976
1977 put_dma_channel(priv);
1978 wret = wait_for_completion_interruptible_timeout(&priv->comp, tmo);
1979
1980 if (wret <= 0) {
1981 rmcd_error("%s(%d) failed waiting for DMA release err=%ld",
1982 current->comm, task_pid_nr(current), wret);
1983 }
1984
Alexandre Bouninee8de3702016-03-22 14:27:08 -07001985 if (priv->dmach != priv->md->dma_chan) {
1986 rmcd_debug(EXIT, "Release DMA channel for filp=%p %s(%d)",
1987 filp, current->comm, task_pid_nr(current));
1988 rio_release_dma(priv->dmach);
1989 } else {
1990 rmcd_debug(EXIT, "Adjust default DMA channel refcount");
1991 kref_put(&md->dma_ref, mport_release_def_dma);
1992 }
1993
1994 priv->dmach = NULL;
1995}
1996#else
1997#define mport_cdev_release_dma(priv) do {} while (0)
1998#endif
1999
2000/*
2001 * mport_cdev_release() - Release character device
2002 */
2003static int mport_cdev_release(struct inode *inode, struct file *filp)
2004{
2005 struct mport_cdev_priv *priv = filp->private_data;
2006 struct mport_dev *chdev;
2007 struct rio_mport_pw_filter *pw_filter, *pw_filter_next;
2008 struct rio_mport_db_filter *db_filter, *db_filter_next;
2009 struct rio_mport_mapping *map, *_map;
2010 unsigned long flags;
2011
2012 rmcd_debug(EXIT, "%s filp=%p", dev_name(&priv->md->dev), filp);
2013
2014 chdev = priv->md;
2015 mport_cdev_release_dma(filp);
2016
2017 priv->event_mask = 0;
2018
2019 spin_lock_irqsave(&chdev->pw_lock, flags);
2020 if (!list_empty(&priv->pw_filters)) {
2021 list_for_each_entry_safe(pw_filter, pw_filter_next,
2022 &priv->pw_filters, priv_node)
2023 rio_mport_delete_pw_filter(pw_filter);
2024 }
2025 spin_unlock_irqrestore(&chdev->pw_lock, flags);
2026
2027 spin_lock_irqsave(&chdev->db_lock, flags);
2028 list_for_each_entry_safe(db_filter, db_filter_next,
2029 &priv->db_filters, priv_node) {
2030 rio_mport_delete_db_filter(db_filter);
2031 }
2032 spin_unlock_irqrestore(&chdev->db_lock, flags);
2033
2034 kfifo_free(&priv->event_fifo);
2035
2036 mutex_lock(&chdev->buf_mutex);
2037 list_for_each_entry_safe(map, _map, &chdev->mappings, node) {
2038 if (map->filp == filp) {
2039 rmcd_debug(EXIT, "release mapping %p filp=%p",
2040 map->virt_addr, filp);
2041 kref_put(&map->ref, mport_release_mapping);
2042 }
2043 }
2044 mutex_unlock(&chdev->buf_mutex);
2045
2046 mport_cdev_fasync(-1, filp, 0);
2047 filp->private_data = NULL;
2048 mutex_lock(&chdev->file_mutex);
2049 list_del(&priv->list);
2050 mutex_unlock(&chdev->file_mutex);
2051 put_device(&chdev->dev);
2052 kfree(priv);
2053 return 0;
2054}
2055
2056/*
2057 * mport_cdev_ioctl() - IOCTLs for character device
2058 */
2059static long mport_cdev_ioctl(struct file *filp,
2060 unsigned int cmd, unsigned long arg)
2061{
2062 int err = -EINVAL;
2063 struct mport_cdev_priv *data = filp->private_data;
2064 struct mport_dev *md = data->md;
2065
2066 if (atomic_read(&md->active) == 0)
2067 return -ENODEV;
2068
2069 switch (cmd) {
2070 case RIO_MPORT_MAINT_READ_LOCAL:
2071 return rio_mport_maint_rd(data, (void __user *)arg, 1);
2072 case RIO_MPORT_MAINT_WRITE_LOCAL:
2073 return rio_mport_maint_wr(data, (void __user *)arg, 1);
2074 case RIO_MPORT_MAINT_READ_REMOTE:
2075 return rio_mport_maint_rd(data, (void __user *)arg, 0);
2076 case RIO_MPORT_MAINT_WRITE_REMOTE:
2077 return rio_mport_maint_wr(data, (void __user *)arg, 0);
2078 case RIO_MPORT_MAINT_HDID_SET:
2079 return maint_hdid_set(data, (void __user *)arg);
2080 case RIO_MPORT_MAINT_COMPTAG_SET:
2081 return maint_comptag_set(data, (void __user *)arg);
2082 case RIO_MPORT_MAINT_PORT_IDX_GET:
2083 return maint_port_idx_get(data, (void __user *)arg);
2084 case RIO_MPORT_GET_PROPERTIES:
2085 md->properties.hdid = md->mport->host_deviceid;
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07002086 if (copy_to_user((void __user *)arg, &(md->properties),
2087 sizeof(md->properties)))
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002088 return -EFAULT;
2089 return 0;
2090 case RIO_ENABLE_DOORBELL_RANGE:
2091 return rio_mport_add_db_filter(data, (void __user *)arg);
2092 case RIO_DISABLE_DOORBELL_RANGE:
2093 return rio_mport_remove_db_filter(data, (void __user *)arg);
2094 case RIO_ENABLE_PORTWRITE_RANGE:
2095 return rio_mport_add_pw_filter(data, (void __user *)arg);
2096 case RIO_DISABLE_PORTWRITE_RANGE:
2097 return rio_mport_remove_pw_filter(data, (void __user *)arg);
2098 case RIO_SET_EVENT_MASK:
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07002099 data->event_mask = (u32)arg;
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002100 return 0;
2101 case RIO_GET_EVENT_MASK:
2102 if (copy_to_user((void __user *)arg, &data->event_mask,
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07002103 sizeof(u32)))
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002104 return -EFAULT;
2105 return 0;
2106 case RIO_MAP_OUTBOUND:
2107 return rio_mport_obw_map(filp, (void __user *)arg);
2108 case RIO_MAP_INBOUND:
2109 return rio_mport_map_inbound(filp, (void __user *)arg);
2110 case RIO_UNMAP_OUTBOUND:
2111 return rio_mport_obw_free(filp, (void __user *)arg);
2112 case RIO_UNMAP_INBOUND:
2113 return rio_mport_inbound_free(filp, (void __user *)arg);
2114 case RIO_ALLOC_DMA:
2115 return rio_mport_alloc_dma(filp, (void __user *)arg);
2116 case RIO_FREE_DMA:
2117 return rio_mport_free_dma(filp, (void __user *)arg);
2118 case RIO_WAIT_FOR_ASYNC:
2119 return rio_mport_wait_for_async_dma(filp, (void __user *)arg);
2120 case RIO_TRANSFER:
2121 return rio_mport_transfer_ioctl(filp, (void __user *)arg);
2122 case RIO_DEV_ADD:
2123 return rio_mport_add_riodev(data, (void __user *)arg);
2124 case RIO_DEV_DEL:
2125 return rio_mport_del_riodev(data, (void __user *)arg);
2126 default:
2127 break;
2128 }
2129
2130 return err;
2131}
2132
2133/*
2134 * mport_release_mapping - free mapping resources and info structure
2135 * @ref: a pointer to the kref within struct rio_mport_mapping
2136 *
2137 * NOTE: Shall be called while holding buf_mutex.
2138 */
2139static void mport_release_mapping(struct kref *ref)
2140{
2141 struct rio_mport_mapping *map =
2142 container_of(ref, struct rio_mport_mapping, ref);
2143 struct rio_mport *mport = map->md->mport;
2144
2145 rmcd_debug(MMAP, "type %d mapping @ %p (phys = %pad) for %s",
2146 map->dir, map->virt_addr,
2147 &map->phys_addr, mport->name);
2148
2149 list_del(&map->node);
2150
2151 switch (map->dir) {
2152 case MAP_INBOUND:
2153 rio_unmap_inb_region(mport, map->phys_addr);
2154 case MAP_DMA:
2155 dma_free_coherent(mport->dev.parent, map->size,
2156 map->virt_addr, map->phys_addr);
2157 break;
2158 case MAP_OUTBOUND:
2159 rio_unmap_outb_region(mport, map->rioid, map->rio_addr);
2160 break;
2161 }
2162 kfree(map);
2163}
2164
2165static void mport_mm_open(struct vm_area_struct *vma)
2166{
2167 struct rio_mport_mapping *map = vma->vm_private_data;
2168
Joe Perchesea87b8e2016-08-02 14:06:28 -07002169 rmcd_debug(MMAP, "%pad", &map->phys_addr);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002170 kref_get(&map->ref);
2171}
2172
2173static void mport_mm_close(struct vm_area_struct *vma)
2174{
2175 struct rio_mport_mapping *map = vma->vm_private_data;
2176
Joe Perchesea87b8e2016-08-02 14:06:28 -07002177 rmcd_debug(MMAP, "%pad", &map->phys_addr);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002178 mutex_lock(&map->md->buf_mutex);
2179 kref_put(&map->ref, mport_release_mapping);
2180 mutex_unlock(&map->md->buf_mutex);
2181}
2182
2183static const struct vm_operations_struct vm_ops = {
2184 .open = mport_mm_open,
2185 .close = mport_mm_close,
2186};
2187
2188static int mport_cdev_mmap(struct file *filp, struct vm_area_struct *vma)
2189{
2190 struct mport_cdev_priv *priv = filp->private_data;
2191 struct mport_dev *md;
2192 size_t size = vma->vm_end - vma->vm_start;
2193 dma_addr_t baddr;
2194 unsigned long offset;
2195 int found = 0, ret;
2196 struct rio_mport_mapping *map;
2197
2198 rmcd_debug(MMAP, "0x%x bytes at offset 0x%lx",
2199 (unsigned int)size, vma->vm_pgoff);
2200
2201 md = priv->md;
2202 baddr = ((dma_addr_t)vma->vm_pgoff << PAGE_SHIFT);
2203
2204 mutex_lock(&md->buf_mutex);
2205 list_for_each_entry(map, &md->mappings, node) {
2206 if (baddr >= map->phys_addr &&
2207 baddr < (map->phys_addr + map->size)) {
2208 found = 1;
2209 break;
2210 }
2211 }
2212 mutex_unlock(&md->buf_mutex);
2213
2214 if (!found)
2215 return -ENOMEM;
2216
2217 offset = baddr - map->phys_addr;
2218
2219 if (size + offset > map->size)
2220 return -EINVAL;
2221
2222 vma->vm_pgoff = offset >> PAGE_SHIFT;
2223 rmcd_debug(MMAP, "MMAP adjusted offset = 0x%lx", vma->vm_pgoff);
2224
2225 if (map->dir == MAP_INBOUND || map->dir == MAP_DMA)
2226 ret = dma_mmap_coherent(md->mport->dev.parent, vma,
2227 map->virt_addr, map->phys_addr, map->size);
2228 else if (map->dir == MAP_OUTBOUND) {
2229 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2230 ret = vm_iomap_memory(vma, map->phys_addr, map->size);
2231 } else {
2232 rmcd_error("Attempt to mmap unsupported mapping type");
2233 ret = -EIO;
2234 }
2235
2236 if (!ret) {
2237 vma->vm_private_data = map;
2238 vma->vm_ops = &vm_ops;
2239 mport_mm_open(vma);
2240 } else {
2241 rmcd_error("MMAP exit with err=%d", ret);
2242 }
2243
2244 return ret;
2245}
2246
Al Viroafc9a422017-07-03 06:39:46 -04002247static __poll_t mport_cdev_poll(struct file *filp, poll_table *wait)
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002248{
2249 struct mport_cdev_priv *priv = filp->private_data;
2250
2251 poll_wait(filp, &priv->event_rx_wait, wait);
2252 if (kfifo_len(&priv->event_fifo))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002253 return EPOLLIN | EPOLLRDNORM;
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002254
2255 return 0;
2256}
2257
2258static ssize_t mport_read(struct file *filp, char __user *buf, size_t count,
2259 loff_t *ppos)
2260{
2261 struct mport_cdev_priv *priv = filp->private_data;
2262 int copied;
2263 ssize_t ret;
2264
2265 if (!count)
2266 return 0;
2267
2268 if (kfifo_is_empty(&priv->event_fifo) &&
2269 (filp->f_flags & O_NONBLOCK))
2270 return -EAGAIN;
2271
2272 if (count % sizeof(struct rio_event))
2273 return -EINVAL;
2274
2275 ret = wait_event_interruptible(priv->event_rx_wait,
2276 kfifo_len(&priv->event_fifo) != 0);
2277 if (ret)
2278 return ret;
2279
2280 while (ret < count) {
2281 if (kfifo_to_user(&priv->event_fifo, buf,
2282 sizeof(struct rio_event), &copied))
2283 return -EFAULT;
2284 ret += copied;
2285 buf += copied;
2286 }
2287
2288 return ret;
2289}
2290
2291static ssize_t mport_write(struct file *filp, const char __user *buf,
2292 size_t count, loff_t *ppos)
2293{
2294 struct mport_cdev_priv *priv = filp->private_data;
2295 struct rio_mport *mport = priv->md->mport;
2296 struct rio_event event;
2297 int len, ret;
2298
2299 if (!count)
2300 return 0;
2301
2302 if (count % sizeof(event))
2303 return -EINVAL;
2304
2305 len = 0;
2306 while ((count - len) >= (int)sizeof(event)) {
2307 if (copy_from_user(&event, buf, sizeof(event)))
2308 return -EFAULT;
2309
2310 if (event.header != RIO_DOORBELL)
2311 return -EINVAL;
2312
2313 ret = rio_mport_send_doorbell(mport,
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07002314 event.u.doorbell.rioid,
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002315 event.u.doorbell.payload);
2316 if (ret < 0)
2317 return ret;
2318
2319 len += sizeof(event);
2320 buf += sizeof(event);
2321 }
2322
2323 return len;
2324}
2325
2326static const struct file_operations mport_fops = {
2327 .owner = THIS_MODULE,
2328 .open = mport_cdev_open,
2329 .release = mport_cdev_release,
2330 .poll = mport_cdev_poll,
2331 .read = mport_read,
2332 .write = mport_write,
2333 .mmap = mport_cdev_mmap,
2334 .fasync = mport_cdev_fasync,
2335 .unlocked_ioctl = mport_cdev_ioctl
2336};
2337
2338/*
2339 * Character device management
2340 */
2341
2342static void mport_device_release(struct device *dev)
2343{
2344 struct mport_dev *md;
2345
2346 rmcd_debug(EXIT, "%s", dev_name(dev));
2347 md = container_of(dev, struct mport_dev, dev);
2348 kfree(md);
2349}
2350
2351/*
2352 * mport_cdev_add() - Create mport_dev from rio_mport
2353 * @mport: RapidIO master port
2354 */
2355static struct mport_dev *mport_cdev_add(struct rio_mport *mport)
2356{
2357 int ret = 0;
2358 struct mport_dev *md;
2359 struct rio_mport_attr attr;
2360
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07002361 md = kzalloc(sizeof(*md), GFP_KERNEL);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002362 if (!md) {
2363 rmcd_error("Unable allocate a device object");
2364 return NULL;
2365 }
2366
2367 md->mport = mport;
2368 mutex_init(&md->buf_mutex);
2369 mutex_init(&md->file_mutex);
2370 INIT_LIST_HEAD(&md->file_list);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002371
Logan Gunthorpedbef3902017-03-17 12:48:20 -06002372 device_initialize(&md->dev);
2373 md->dev.devt = MKDEV(MAJOR(dev_number), mport->id);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002374 md->dev.class = dev_class;
2375 md->dev.parent = &mport->dev;
2376 md->dev.release = mport_device_release;
2377 dev_set_name(&md->dev, DEV_NAME "%d", mport->id);
2378 atomic_set(&md->active, 1);
2379
Logan Gunthorpedbef3902017-03-17 12:48:20 -06002380 cdev_init(&md->cdev, &mport_fops);
2381 md->cdev.owner = THIS_MODULE;
2382
2383 ret = cdev_device_add(&md->cdev, &md->dev);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002384 if (ret) {
2385 rmcd_error("Failed to register mport %d (err=%d)",
2386 mport->id, ret);
2387 goto err_cdev;
2388 }
2389
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002390 INIT_LIST_HEAD(&md->doorbells);
2391 spin_lock_init(&md->db_lock);
2392 INIT_LIST_HEAD(&md->portwrites);
2393 spin_lock_init(&md->pw_lock);
2394 INIT_LIST_HEAD(&md->mappings);
2395
2396 md->properties.id = mport->id;
2397 md->properties.sys_size = mport->sys_size;
2398 md->properties.hdid = mport->host_deviceid;
2399 md->properties.index = mport->index;
2400
2401 /* The transfer_mode property will be returned through mport query
2402 * interface
2403 */
Alexandre Bounine4e1016d2016-05-05 16:22:06 -07002404#ifdef CONFIG_FSL_RIO /* for now: only on Freescale's SoCs */
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002405 md->properties.transfer_mode |= RIO_TRANSFER_MODE_MAPPED;
2406#else
2407 md->properties.transfer_mode |= RIO_TRANSFER_MODE_TRANSFER;
2408#endif
2409 ret = rio_query_mport(mport, &attr);
2410 if (!ret) {
2411 md->properties.flags = attr.flags;
2412 md->properties.link_speed = attr.link_speed;
2413 md->properties.link_width = attr.link_width;
2414 md->properties.dma_max_sge = attr.dma_max_sge;
2415 md->properties.dma_max_size = attr.dma_max_size;
2416 md->properties.dma_align = attr.dma_align;
2417 md->properties.cap_sys_size = 0;
2418 md->properties.cap_transfer_mode = 0;
2419 md->properties.cap_addr_size = 0;
2420 } else
2421 pr_info(DRV_PREFIX "Failed to obtain info for %s cdev(%d:%d)\n",
2422 mport->name, MAJOR(dev_number), mport->id);
2423
2424 mutex_lock(&mport_devs_lock);
2425 list_add_tail(&md->node, &mport_devs);
2426 mutex_unlock(&mport_devs_lock);
2427
2428 pr_info(DRV_PREFIX "Added %s cdev(%d:%d)\n",
2429 mport->name, MAJOR(dev_number), mport->id);
2430
2431 return md;
2432
2433err_cdev:
Logan Gunthorpedbef3902017-03-17 12:48:20 -06002434 put_device(&md->dev);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002435 return NULL;
2436}
2437
2438/*
2439 * mport_cdev_terminate_dma() - Stop all active DMA data transfers and release
2440 * associated DMA channels.
2441 */
2442static void mport_cdev_terminate_dma(struct mport_dev *md)
2443{
2444#ifdef CONFIG_RAPIDIO_DMA_ENGINE
2445 struct mport_cdev_priv *client;
2446
2447 rmcd_debug(DMA, "%s", dev_name(&md->dev));
2448
2449 mutex_lock(&md->file_mutex);
2450 list_for_each_entry(client, &md->file_list, list) {
2451 if (client->dmach) {
2452 dmaengine_terminate_all(client->dmach);
2453 rio_release_dma(client->dmach);
2454 }
2455 }
2456 mutex_unlock(&md->file_mutex);
2457
2458 if (md->dma_chan) {
2459 dmaengine_terminate_all(md->dma_chan);
2460 rio_release_dma(md->dma_chan);
2461 md->dma_chan = NULL;
2462 }
2463#endif
2464}
2465
2466
2467/*
2468 * mport_cdev_kill_fasync() - Send SIGIO signal to all processes with open
2469 * mport_cdev files.
2470 */
2471static int mport_cdev_kill_fasync(struct mport_dev *md)
2472{
2473 unsigned int files = 0;
2474 struct mport_cdev_priv *client;
2475
2476 mutex_lock(&md->file_mutex);
2477 list_for_each_entry(client, &md->file_list, list) {
2478 if (client->async_queue)
2479 kill_fasync(&client->async_queue, SIGIO, POLL_HUP);
2480 files++;
2481 }
2482 mutex_unlock(&md->file_mutex);
2483 return files;
2484}
2485
2486/*
2487 * mport_cdev_remove() - Remove mport character device
2488 * @dev: Mport device to remove
2489 */
2490static void mport_cdev_remove(struct mport_dev *md)
2491{
2492 struct rio_mport_mapping *map, *_map;
2493
2494 rmcd_debug(EXIT, "Remove %s cdev", md->mport->name);
2495 atomic_set(&md->active, 0);
2496 mport_cdev_terminate_dma(md);
2497 rio_del_mport_pw_handler(md->mport, md, rio_mport_pw_handler);
Logan Gunthorpedbef3902017-03-17 12:48:20 -06002498 cdev_device_del(&md->cdev, &md->dev);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002499 mport_cdev_kill_fasync(md);
2500
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002501 /* TODO: do we need to give clients some time to close file
2502 * descriptors? Simple wait for XX, or kref?
2503 */
2504
2505 /*
2506 * Release DMA buffers allocated for the mport device.
2507 * Disable associated inbound Rapidio requests mapping if applicable.
2508 */
2509 mutex_lock(&md->buf_mutex);
2510 list_for_each_entry_safe(map, _map, &md->mappings, node) {
2511 kref_put(&map->ref, mport_release_mapping);
2512 }
2513 mutex_unlock(&md->buf_mutex);
2514
2515 if (!list_empty(&md->mappings))
2516 rmcd_warn("WARNING: %s pending mappings on removal",
2517 md->mport->name);
2518
2519 rio_release_inb_dbell(md->mport, 0, 0x0fff);
2520
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002521 put_device(&md->dev);
2522}
2523
2524/*
2525 * RIO rio_mport_interface driver
2526 */
2527
2528/*
2529 * mport_add_mport() - Add rio_mport from LDM device struct
2530 * @dev: Linux device model struct
2531 * @class_intf: Linux class_interface
2532 */
2533static int mport_add_mport(struct device *dev,
2534 struct class_interface *class_intf)
2535{
2536 struct rio_mport *mport = NULL;
2537 struct mport_dev *chdev = NULL;
2538
2539 mport = to_rio_mport(dev);
2540 if (!mport)
2541 return -ENODEV;
2542
2543 chdev = mport_cdev_add(mport);
2544 if (!chdev)
2545 return -ENODEV;
2546
2547 return 0;
2548}
2549
2550/*
2551 * mport_remove_mport() - Remove rio_mport from global list
2552 * TODO remove device from global mport_dev list
2553 */
2554static void mport_remove_mport(struct device *dev,
2555 struct class_interface *class_intf)
2556{
2557 struct rio_mport *mport = NULL;
2558 struct mport_dev *chdev;
2559 int found = 0;
2560
2561 mport = to_rio_mport(dev);
2562 rmcd_debug(EXIT, "Remove %s", mport->name);
2563
2564 mutex_lock(&mport_devs_lock);
2565 list_for_each_entry(chdev, &mport_devs, node) {
2566 if (chdev->mport->id == mport->id) {
2567 atomic_set(&chdev->active, 0);
2568 list_del(&chdev->node);
2569 found = 1;
2570 break;
2571 }
2572 }
2573 mutex_unlock(&mport_devs_lock);
2574
2575 if (found)
2576 mport_cdev_remove(chdev);
2577}
2578
2579/* the rio_mport_interface is used to handle local mport devices */
2580static struct class_interface rio_mport_interface __refdata = {
2581 .class = &rio_mport_class,
2582 .add_dev = mport_add_mport,
2583 .remove_dev = mport_remove_mport,
2584};
2585
2586/*
2587 * Linux kernel module
2588 */
2589
2590/*
2591 * mport_init - Driver module loading
2592 */
2593static int __init mport_init(void)
2594{
2595 int ret;
2596
2597 /* Create device class needed by udev */
2598 dev_class = class_create(THIS_MODULE, DRV_NAME);
Vladimir Zapolskiy99f23c22016-04-28 16:19:06 -07002599 if (IS_ERR(dev_class)) {
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002600 rmcd_error("Unable to create " DRV_NAME " class");
Vladimir Zapolskiy99f23c22016-04-28 16:19:06 -07002601 return PTR_ERR(dev_class);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002602 }
2603
2604 ret = alloc_chrdev_region(&dev_number, 0, RIO_MAX_MPORTS, DRV_NAME);
2605 if (ret < 0)
2606 goto err_chr;
2607
2608 rmcd_debug(INIT, "Registered class with major=%d", MAJOR(dev_number));
2609
2610 /* Register to rio_mport_interface */
2611 ret = class_interface_register(&rio_mport_interface);
2612 if (ret) {
2613 rmcd_error("class_interface_register() failed, err=%d", ret);
2614 goto err_cli;
2615 }
2616
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002617 return 0;
2618
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002619err_cli:
2620 unregister_chrdev_region(dev_number, RIO_MAX_MPORTS);
2621err_chr:
2622 class_destroy(dev_class);
2623 return ret;
2624}
2625
2626/**
2627 * mport_exit - Driver module unloading
2628 */
2629static void __exit mport_exit(void)
2630{
2631 class_interface_unregister(&rio_mport_interface);
2632 class_destroy(dev_class);
2633 unregister_chrdev_region(dev_number, RIO_MAX_MPORTS);
Alexandre Bouninee8de3702016-03-22 14:27:08 -07002634}
2635
2636module_init(mport_init);
2637module_exit(mport_exit);