rio_mport_cdev.c 65 KB

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