rio_mport_cdev.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637
  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 mport_dev *md;
  869. struct rio_async_tx_wait w_param;
  870. struct mport_dma_req *req;
  871. dma_cookie_t cookie;
  872. unsigned long tmo;
  873. long wret;
  874. int found = 0;
  875. int ret;
  876. priv = (struct mport_cdev_priv *)filp->private_data;
  877. md = priv->md;
  878. if (unlikely(copy_from_user(&w_param, arg, sizeof(w_param))))
  879. return -EFAULT;
  880. cookie = w_param.token;
  881. if (w_param.timeout)
  882. tmo = msecs_to_jiffies(w_param.timeout);
  883. else /* Use default DMA timeout */
  884. tmo = msecs_to_jiffies(dma_timeout);
  885. spin_lock(&priv->req_lock);
  886. list_for_each_entry(req, &priv->async_list, node) {
  887. if (req->cookie == cookie) {
  888. list_del(&req->node);
  889. found = 1;
  890. break;
  891. }
  892. }
  893. spin_unlock(&priv->req_lock);
  894. if (!found)
  895. return -EAGAIN;
  896. wret = wait_for_completion_interruptible_timeout(&req->req_comp, tmo);
  897. if (wret == 0) {
  898. /* Timeout on wait occurred */
  899. rmcd_error("%s(%d) timed out waiting for ASYNC DMA_%s",
  900. current->comm, task_pid_nr(current),
  901. (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE");
  902. ret = -ETIMEDOUT;
  903. goto err_tmo;
  904. } else if (wret == -ERESTARTSYS) {
  905. /* Wait_for_completion was interrupted by a signal but DMA may
  906. * be still in progress
  907. */
  908. rmcd_error("%s(%d) wait for ASYNC DMA_%s was interrupted",
  909. current->comm, task_pid_nr(current),
  910. (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE");
  911. ret = -EINTR;
  912. goto err_tmo;
  913. }
  914. if (req->status != DMA_COMPLETE) {
  915. /* DMA transaction completion signaled with transfer error */
  916. rmcd_error("%s(%d) ASYNC DMA_%s completion with status %d",
  917. current->comm, task_pid_nr(current),
  918. (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE",
  919. req->status);
  920. ret = -EIO;
  921. } else
  922. ret = 0;
  923. if (req->status != DMA_IN_PROGRESS && req->status != DMA_PAUSED)
  924. kref_put(&req->refcount, dma_req_free);
  925. return ret;
  926. err_tmo:
  927. /* Return request back into async queue */
  928. spin_lock(&priv->req_lock);
  929. list_add_tail(&req->node, &priv->async_list);
  930. spin_unlock(&priv->req_lock);
  931. return ret;
  932. }
  933. static int rio_mport_create_dma_mapping(struct mport_dev *md, struct file *filp,
  934. u64 size, struct rio_mport_mapping **mapping)
  935. {
  936. struct rio_mport_mapping *map;
  937. map = kzalloc(sizeof(*map), GFP_KERNEL);
  938. if (map == NULL)
  939. return -ENOMEM;
  940. map->virt_addr = dma_alloc_coherent(md->mport->dev.parent, size,
  941. &map->phys_addr, GFP_KERNEL);
  942. if (map->virt_addr == NULL) {
  943. kfree(map);
  944. return -ENOMEM;
  945. }
  946. map->dir = MAP_DMA;
  947. map->size = size;
  948. map->filp = filp;
  949. map->md = md;
  950. kref_init(&map->ref);
  951. mutex_lock(&md->buf_mutex);
  952. list_add_tail(&map->node, &md->mappings);
  953. mutex_unlock(&md->buf_mutex);
  954. *mapping = map;
  955. return 0;
  956. }
  957. static int rio_mport_alloc_dma(struct file *filp, void __user *arg)
  958. {
  959. struct mport_cdev_priv *priv = filp->private_data;
  960. struct mport_dev *md = priv->md;
  961. struct rio_dma_mem map;
  962. struct rio_mport_mapping *mapping = NULL;
  963. int ret;
  964. if (unlikely(copy_from_user(&map, arg, sizeof(map))))
  965. return -EFAULT;
  966. ret = rio_mport_create_dma_mapping(md, filp, map.length, &mapping);
  967. if (ret)
  968. return ret;
  969. map.dma_handle = mapping->phys_addr;
  970. if (unlikely(copy_to_user(arg, &map, sizeof(map)))) {
  971. mutex_lock(&md->buf_mutex);
  972. kref_put(&mapping->ref, mport_release_mapping);
  973. mutex_unlock(&md->buf_mutex);
  974. return -EFAULT;
  975. }
  976. return 0;
  977. }
  978. static int rio_mport_free_dma(struct file *filp, void __user *arg)
  979. {
  980. struct mport_cdev_priv *priv = filp->private_data;
  981. struct mport_dev *md = priv->md;
  982. u64 handle;
  983. int ret = -EFAULT;
  984. struct rio_mport_mapping *map, *_map;
  985. if (copy_from_user(&handle, arg, sizeof(handle)))
  986. return -EFAULT;
  987. rmcd_debug(EXIT, "filp=%p", filp);
  988. mutex_lock(&md->buf_mutex);
  989. list_for_each_entry_safe(map, _map, &md->mappings, node) {
  990. if (map->dir == MAP_DMA && map->phys_addr == handle &&
  991. map->filp == filp) {
  992. kref_put(&map->ref, mport_release_mapping);
  993. ret = 0;
  994. break;
  995. }
  996. }
  997. mutex_unlock(&md->buf_mutex);
  998. if (ret == -EFAULT) {
  999. rmcd_debug(DMA, "ERR no matching mapping");
  1000. return ret;
  1001. }
  1002. return 0;
  1003. }
  1004. #else
  1005. static int rio_mport_transfer_ioctl(struct file *filp, void *arg)
  1006. {
  1007. return -ENODEV;
  1008. }
  1009. static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg)
  1010. {
  1011. return -ENODEV;
  1012. }
  1013. static int rio_mport_alloc_dma(struct file *filp, void __user *arg)
  1014. {
  1015. return -ENODEV;
  1016. }
  1017. static int rio_mport_free_dma(struct file *filp, void __user *arg)
  1018. {
  1019. return -ENODEV;
  1020. }
  1021. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  1022. /*
  1023. * Inbound/outbound memory mapping functions
  1024. */
  1025. static int
  1026. rio_mport_create_inbound_mapping(struct mport_dev *md, struct file *filp,
  1027. u64 raddr, u64 size,
  1028. struct rio_mport_mapping **mapping)
  1029. {
  1030. struct rio_mport *mport = md->mport;
  1031. struct rio_mport_mapping *map;
  1032. int ret;
  1033. /* rio_map_inb_region() accepts u32 size */
  1034. if (size > 0xffffffff)
  1035. return -EINVAL;
  1036. map = kzalloc(sizeof(*map), GFP_KERNEL);
  1037. if (map == NULL)
  1038. return -ENOMEM;
  1039. map->virt_addr = dma_alloc_coherent(mport->dev.parent, size,
  1040. &map->phys_addr, GFP_KERNEL);
  1041. if (map->virt_addr == NULL) {
  1042. ret = -ENOMEM;
  1043. goto err_dma_alloc;
  1044. }
  1045. if (raddr == RIO_MAP_ANY_ADDR)
  1046. raddr = map->phys_addr;
  1047. ret = rio_map_inb_region(mport, map->phys_addr, raddr, (u32)size, 0);
  1048. if (ret < 0)
  1049. goto err_map_inb;
  1050. map->dir = MAP_INBOUND;
  1051. map->rio_addr = raddr;
  1052. map->size = size;
  1053. map->filp = filp;
  1054. map->md = md;
  1055. kref_init(&map->ref);
  1056. mutex_lock(&md->buf_mutex);
  1057. list_add_tail(&map->node, &md->mappings);
  1058. mutex_unlock(&md->buf_mutex);
  1059. *mapping = map;
  1060. return 0;
  1061. err_map_inb:
  1062. dma_free_coherent(mport->dev.parent, size,
  1063. map->virt_addr, map->phys_addr);
  1064. err_dma_alloc:
  1065. kfree(map);
  1066. return ret;
  1067. }
  1068. static int
  1069. rio_mport_get_inbound_mapping(struct mport_dev *md, struct file *filp,
  1070. u64 raddr, u64 size,
  1071. struct rio_mport_mapping **mapping)
  1072. {
  1073. struct rio_mport_mapping *map;
  1074. int err = -ENOMEM;
  1075. if (raddr == RIO_MAP_ANY_ADDR)
  1076. goto get_new;
  1077. mutex_lock(&md->buf_mutex);
  1078. list_for_each_entry(map, &md->mappings, node) {
  1079. if (map->dir != MAP_INBOUND)
  1080. continue;
  1081. if (raddr == map->rio_addr && size == map->size) {
  1082. /* allow exact match only */
  1083. *mapping = map;
  1084. err = 0;
  1085. break;
  1086. } else if (raddr < (map->rio_addr + map->size - 1) &&
  1087. (raddr + size) > map->rio_addr) {
  1088. err = -EBUSY;
  1089. break;
  1090. }
  1091. }
  1092. mutex_unlock(&md->buf_mutex);
  1093. if (err != -ENOMEM)
  1094. return err;
  1095. get_new:
  1096. /* not found, create new */
  1097. return rio_mport_create_inbound_mapping(md, filp, raddr, size, mapping);
  1098. }
  1099. static int rio_mport_map_inbound(struct file *filp, void __user *arg)
  1100. {
  1101. struct mport_cdev_priv *priv = filp->private_data;
  1102. struct mport_dev *md = priv->md;
  1103. struct rio_mmap map;
  1104. struct rio_mport_mapping *mapping = NULL;
  1105. int ret;
  1106. if (!md->mport->ops->map_inb)
  1107. return -EPROTONOSUPPORT;
  1108. if (unlikely(copy_from_user(&map, arg, sizeof(map))))
  1109. return -EFAULT;
  1110. rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp);
  1111. ret = rio_mport_get_inbound_mapping(md, filp, map.rio_addr,
  1112. map.length, &mapping);
  1113. if (ret)
  1114. return ret;
  1115. map.handle = mapping->phys_addr;
  1116. map.rio_addr = mapping->rio_addr;
  1117. if (unlikely(copy_to_user(arg, &map, sizeof(map)))) {
  1118. /* Delete mapping if it was created by this request */
  1119. if (ret == 0 && mapping->filp == filp) {
  1120. mutex_lock(&md->buf_mutex);
  1121. kref_put(&mapping->ref, mport_release_mapping);
  1122. mutex_unlock(&md->buf_mutex);
  1123. }
  1124. return -EFAULT;
  1125. }
  1126. return 0;
  1127. }
  1128. /*
  1129. * rio_mport_inbound_free() - unmap from RapidIO address space and free
  1130. * previously allocated inbound DMA coherent buffer
  1131. * @priv: driver private data
  1132. * @arg: buffer handle returned by allocation routine
  1133. */
  1134. static int rio_mport_inbound_free(struct file *filp, void __user *arg)
  1135. {
  1136. struct mport_cdev_priv *priv = filp->private_data;
  1137. struct mport_dev *md = priv->md;
  1138. u64 handle;
  1139. struct rio_mport_mapping *map, *_map;
  1140. rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp);
  1141. if (!md->mport->ops->unmap_inb)
  1142. return -EPROTONOSUPPORT;
  1143. if (copy_from_user(&handle, arg, sizeof(handle)))
  1144. return -EFAULT;
  1145. mutex_lock(&md->buf_mutex);
  1146. list_for_each_entry_safe(map, _map, &md->mappings, node) {
  1147. if (map->dir == MAP_INBOUND && map->phys_addr == handle) {
  1148. if (map->filp == filp) {
  1149. map->filp = NULL;
  1150. kref_put(&map->ref, mport_release_mapping);
  1151. }
  1152. break;
  1153. }
  1154. }
  1155. mutex_unlock(&md->buf_mutex);
  1156. return 0;
  1157. }
  1158. /*
  1159. * maint_port_idx_get() - Get the port index of the mport instance
  1160. * @priv: driver private data
  1161. * @arg: port index
  1162. */
  1163. static int maint_port_idx_get(struct mport_cdev_priv *priv, void __user *arg)
  1164. {
  1165. struct mport_dev *md = priv->md;
  1166. u32 port_idx = md->mport->index;
  1167. rmcd_debug(MPORT, "port_index=%d", port_idx);
  1168. if (copy_to_user(arg, &port_idx, sizeof(port_idx)))
  1169. return -EFAULT;
  1170. return 0;
  1171. }
  1172. static int rio_mport_add_event(struct mport_cdev_priv *priv,
  1173. struct rio_event *event)
  1174. {
  1175. int overflow;
  1176. if (!(priv->event_mask & event->header))
  1177. return -EACCES;
  1178. spin_lock(&priv->fifo_lock);
  1179. overflow = kfifo_avail(&priv->event_fifo) < sizeof(*event)
  1180. || kfifo_in(&priv->event_fifo, (unsigned char *)event,
  1181. sizeof(*event)) != sizeof(*event);
  1182. spin_unlock(&priv->fifo_lock);
  1183. wake_up_interruptible(&priv->event_rx_wait);
  1184. if (overflow) {
  1185. dev_warn(&priv->md->dev, DRV_NAME ": event fifo overflow\n");
  1186. return -EBUSY;
  1187. }
  1188. return 0;
  1189. }
  1190. static void rio_mport_doorbell_handler(struct rio_mport *mport, void *dev_id,
  1191. u16 src, u16 dst, u16 info)
  1192. {
  1193. struct mport_dev *data = dev_id;
  1194. struct mport_cdev_priv *priv;
  1195. struct rio_mport_db_filter *db_filter;
  1196. struct rio_event event;
  1197. int handled;
  1198. event.header = RIO_DOORBELL;
  1199. event.u.doorbell.rioid = src;
  1200. event.u.doorbell.payload = info;
  1201. handled = 0;
  1202. spin_lock(&data->db_lock);
  1203. list_for_each_entry(db_filter, &data->doorbells, data_node) {
  1204. if (((db_filter->filter.rioid == RIO_INVALID_DESTID ||
  1205. db_filter->filter.rioid == src)) &&
  1206. info >= db_filter->filter.low &&
  1207. info <= db_filter->filter.high) {
  1208. priv = db_filter->priv;
  1209. rio_mport_add_event(priv, &event);
  1210. handled = 1;
  1211. }
  1212. }
  1213. spin_unlock(&data->db_lock);
  1214. if (!handled)
  1215. dev_warn(&data->dev,
  1216. "%s: spurious DB received from 0x%x, info=0x%04x\n",
  1217. __func__, src, info);
  1218. }
  1219. static int rio_mport_add_db_filter(struct mport_cdev_priv *priv,
  1220. void __user *arg)
  1221. {
  1222. struct mport_dev *md = priv->md;
  1223. struct rio_mport_db_filter *db_filter;
  1224. struct rio_doorbell_filter filter;
  1225. unsigned long flags;
  1226. int ret;
  1227. if (copy_from_user(&filter, arg, sizeof(filter)))
  1228. return -EFAULT;
  1229. if (filter.low > filter.high)
  1230. return -EINVAL;
  1231. ret = rio_request_inb_dbell(md->mport, md, filter.low, filter.high,
  1232. rio_mport_doorbell_handler);
  1233. if (ret) {
  1234. rmcd_error("%s failed to register IBDB, err=%d",
  1235. dev_name(&md->dev), ret);
  1236. return ret;
  1237. }
  1238. db_filter = kzalloc(sizeof(*db_filter), GFP_KERNEL);
  1239. if (db_filter == NULL) {
  1240. rio_release_inb_dbell(md->mport, filter.low, filter.high);
  1241. return -ENOMEM;
  1242. }
  1243. db_filter->filter = filter;
  1244. db_filter->priv = priv;
  1245. spin_lock_irqsave(&md->db_lock, flags);
  1246. list_add_tail(&db_filter->priv_node, &priv->db_filters);
  1247. list_add_tail(&db_filter->data_node, &md->doorbells);
  1248. spin_unlock_irqrestore(&md->db_lock, flags);
  1249. return 0;
  1250. }
  1251. static void rio_mport_delete_db_filter(struct rio_mport_db_filter *db_filter)
  1252. {
  1253. list_del(&db_filter->data_node);
  1254. list_del(&db_filter->priv_node);
  1255. kfree(db_filter);
  1256. }
  1257. static int rio_mport_remove_db_filter(struct mport_cdev_priv *priv,
  1258. void __user *arg)
  1259. {
  1260. struct rio_mport_db_filter *db_filter;
  1261. struct rio_doorbell_filter filter;
  1262. unsigned long flags;
  1263. int ret = -EINVAL;
  1264. if (copy_from_user(&filter, arg, sizeof(filter)))
  1265. return -EFAULT;
  1266. if (filter.low > filter.high)
  1267. return -EINVAL;
  1268. spin_lock_irqsave(&priv->md->db_lock, flags);
  1269. list_for_each_entry(db_filter, &priv->db_filters, priv_node) {
  1270. if (db_filter->filter.rioid == filter.rioid &&
  1271. db_filter->filter.low == filter.low &&
  1272. db_filter->filter.high == filter.high) {
  1273. rio_mport_delete_db_filter(db_filter);
  1274. ret = 0;
  1275. break;
  1276. }
  1277. }
  1278. spin_unlock_irqrestore(&priv->md->db_lock, flags);
  1279. if (!ret)
  1280. rio_release_inb_dbell(priv->md->mport, filter.low, filter.high);
  1281. return ret;
  1282. }
  1283. static int rio_mport_match_pw(union rio_pw_msg *msg,
  1284. struct rio_pw_filter *filter)
  1285. {
  1286. if ((msg->em.comptag & filter->mask) < filter->low ||
  1287. (msg->em.comptag & filter->mask) > filter->high)
  1288. return 0;
  1289. return 1;
  1290. }
  1291. static int rio_mport_pw_handler(struct rio_mport *mport, void *context,
  1292. union rio_pw_msg *msg, int step)
  1293. {
  1294. struct mport_dev *md = context;
  1295. struct mport_cdev_priv *priv;
  1296. struct rio_mport_pw_filter *pw_filter;
  1297. struct rio_event event;
  1298. int handled;
  1299. event.header = RIO_PORTWRITE;
  1300. memcpy(event.u.portwrite.payload, msg->raw, RIO_PW_MSG_SIZE);
  1301. handled = 0;
  1302. spin_lock(&md->pw_lock);
  1303. list_for_each_entry(pw_filter, &md->portwrites, md_node) {
  1304. if (rio_mport_match_pw(msg, &pw_filter->filter)) {
  1305. priv = pw_filter->priv;
  1306. rio_mport_add_event(priv, &event);
  1307. handled = 1;
  1308. }
  1309. }
  1310. spin_unlock(&md->pw_lock);
  1311. if (!handled) {
  1312. printk_ratelimited(KERN_WARNING DRV_NAME
  1313. ": mport%d received spurious PW from 0x%08x\n",
  1314. mport->id, msg->em.comptag);
  1315. }
  1316. return 0;
  1317. }
  1318. static int rio_mport_add_pw_filter(struct mport_cdev_priv *priv,
  1319. void __user *arg)
  1320. {
  1321. struct mport_dev *md = priv->md;
  1322. struct rio_mport_pw_filter *pw_filter;
  1323. struct rio_pw_filter filter;
  1324. unsigned long flags;
  1325. int hadd = 0;
  1326. if (copy_from_user(&filter, arg, sizeof(filter)))
  1327. return -EFAULT;
  1328. pw_filter = kzalloc(sizeof(*pw_filter), GFP_KERNEL);
  1329. if (pw_filter == NULL)
  1330. return -ENOMEM;
  1331. pw_filter->filter = filter;
  1332. pw_filter->priv = priv;
  1333. spin_lock_irqsave(&md->pw_lock, flags);
  1334. if (list_empty(&md->portwrites))
  1335. hadd = 1;
  1336. list_add_tail(&pw_filter->priv_node, &priv->pw_filters);
  1337. list_add_tail(&pw_filter->md_node, &md->portwrites);
  1338. spin_unlock_irqrestore(&md->pw_lock, flags);
  1339. if (hadd) {
  1340. int ret;
  1341. ret = rio_add_mport_pw_handler(md->mport, md,
  1342. rio_mport_pw_handler);
  1343. if (ret) {
  1344. dev_err(&md->dev,
  1345. "%s: failed to add IB_PW handler, err=%d\n",
  1346. __func__, ret);
  1347. return ret;
  1348. }
  1349. rio_pw_enable(md->mport, 1);
  1350. }
  1351. return 0;
  1352. }
  1353. static void rio_mport_delete_pw_filter(struct rio_mport_pw_filter *pw_filter)
  1354. {
  1355. list_del(&pw_filter->md_node);
  1356. list_del(&pw_filter->priv_node);
  1357. kfree(pw_filter);
  1358. }
  1359. static int rio_mport_match_pw_filter(struct rio_pw_filter *a,
  1360. struct rio_pw_filter *b)
  1361. {
  1362. if ((a->mask == b->mask) && (a->low == b->low) && (a->high == b->high))
  1363. return 1;
  1364. return 0;
  1365. }
  1366. static int rio_mport_remove_pw_filter(struct mport_cdev_priv *priv,
  1367. void __user *arg)
  1368. {
  1369. struct mport_dev *md = priv->md;
  1370. struct rio_mport_pw_filter *pw_filter;
  1371. struct rio_pw_filter filter;
  1372. unsigned long flags;
  1373. int ret = -EINVAL;
  1374. int hdel = 0;
  1375. if (copy_from_user(&filter, arg, sizeof(filter)))
  1376. return -EFAULT;
  1377. spin_lock_irqsave(&md->pw_lock, flags);
  1378. list_for_each_entry(pw_filter, &priv->pw_filters, priv_node) {
  1379. if (rio_mport_match_pw_filter(&pw_filter->filter, &filter)) {
  1380. rio_mport_delete_pw_filter(pw_filter);
  1381. ret = 0;
  1382. break;
  1383. }
  1384. }
  1385. if (list_empty(&md->portwrites))
  1386. hdel = 1;
  1387. spin_unlock_irqrestore(&md->pw_lock, flags);
  1388. if (hdel) {
  1389. rio_del_mport_pw_handler(md->mport, priv->md,
  1390. rio_mport_pw_handler);
  1391. rio_pw_enable(md->mport, 0);
  1392. }
  1393. return ret;
  1394. }
  1395. /*
  1396. * rio_release_dev - release routine for kernel RIO device object
  1397. * @dev: kernel device object associated with a RIO device structure
  1398. *
  1399. * Frees a RIO device struct associated a RIO device struct.
  1400. * The RIO device struct is freed.
  1401. */
  1402. static void rio_release_dev(struct device *dev)
  1403. {
  1404. struct rio_dev *rdev;
  1405. rdev = to_rio_dev(dev);
  1406. pr_info(DRV_PREFIX "%s: %s\n", __func__, rio_name(rdev));
  1407. kfree(rdev);
  1408. }
  1409. static void rio_release_net(struct device *dev)
  1410. {
  1411. struct rio_net *net;
  1412. net = to_rio_net(dev);
  1413. rmcd_debug(RDEV, "net_%d", net->id);
  1414. kfree(net);
  1415. }
  1416. /*
  1417. * rio_mport_add_riodev - creates a kernel RIO device object
  1418. *
  1419. * Allocates a RIO device data structure and initializes required fields based
  1420. * on device's configuration space contents.
  1421. * If the device has switch capabilities, then a switch specific portion is
  1422. * allocated and configured.
  1423. */
  1424. static int rio_mport_add_riodev(struct mport_cdev_priv *priv,
  1425. void __user *arg)
  1426. {
  1427. struct mport_dev *md = priv->md;
  1428. struct rio_rdev_info dev_info;
  1429. struct rio_dev *rdev;
  1430. struct rio_switch *rswitch = NULL;
  1431. struct rio_mport *mport;
  1432. size_t size;
  1433. u32 rval;
  1434. u32 swpinfo = 0;
  1435. u16 destid;
  1436. u8 hopcount;
  1437. int err;
  1438. if (copy_from_user(&dev_info, arg, sizeof(dev_info)))
  1439. return -EFAULT;
  1440. rmcd_debug(RDEV, "name:%s ct:0x%x did:0x%x hc:0x%x", dev_info.name,
  1441. dev_info.comptag, dev_info.destid, dev_info.hopcount);
  1442. if (bus_find_device_by_name(&rio_bus_type, NULL, dev_info.name)) {
  1443. rmcd_debug(RDEV, "device %s already exists", dev_info.name);
  1444. return -EEXIST;
  1445. }
  1446. size = sizeof(*rdev);
  1447. mport = md->mport;
  1448. destid = dev_info.destid;
  1449. hopcount = dev_info.hopcount;
  1450. if (rio_mport_read_config_32(mport, destid, hopcount,
  1451. RIO_PEF_CAR, &rval))
  1452. return -EIO;
  1453. if (rval & RIO_PEF_SWITCH) {
  1454. rio_mport_read_config_32(mport, destid, hopcount,
  1455. RIO_SWP_INFO_CAR, &swpinfo);
  1456. size += (RIO_GET_TOTAL_PORTS(swpinfo) *
  1457. sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
  1458. }
  1459. rdev = kzalloc(size, GFP_KERNEL);
  1460. if (rdev == NULL)
  1461. return -ENOMEM;
  1462. if (mport->net == NULL) {
  1463. struct rio_net *net;
  1464. net = rio_alloc_net(mport);
  1465. if (!net) {
  1466. err = -ENOMEM;
  1467. rmcd_debug(RDEV, "failed to allocate net object");
  1468. goto cleanup;
  1469. }
  1470. net->id = mport->id;
  1471. net->hport = mport;
  1472. dev_set_name(&net->dev, "rnet_%d", net->id);
  1473. net->dev.parent = &mport->dev;
  1474. net->dev.release = rio_release_net;
  1475. err = rio_add_net(net);
  1476. if (err) {
  1477. rmcd_debug(RDEV, "failed to register net, err=%d", err);
  1478. kfree(net);
  1479. goto cleanup;
  1480. }
  1481. }
  1482. rdev->net = mport->net;
  1483. rdev->pef = rval;
  1484. rdev->swpinfo = swpinfo;
  1485. rio_mport_read_config_32(mport, destid, hopcount,
  1486. RIO_DEV_ID_CAR, &rval);
  1487. rdev->did = rval >> 16;
  1488. rdev->vid = rval & 0xffff;
  1489. rio_mport_read_config_32(mport, destid, hopcount, RIO_DEV_INFO_CAR,
  1490. &rdev->device_rev);
  1491. rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_ID_CAR,
  1492. &rval);
  1493. rdev->asm_did = rval >> 16;
  1494. rdev->asm_vid = rval & 0xffff;
  1495. rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_INFO_CAR,
  1496. &rval);
  1497. rdev->asm_rev = rval >> 16;
  1498. if (rdev->pef & RIO_PEF_EXT_FEATURES) {
  1499. rdev->efptr = rval & 0xffff;
  1500. rdev->phys_efptr = rio_mport_get_physefb(mport, 0, destid,
  1501. hopcount, &rdev->phys_rmap);
  1502. rdev->em_efptr = rio_mport_get_feature(mport, 0, destid,
  1503. hopcount, RIO_EFB_ERR_MGMNT);
  1504. }
  1505. rio_mport_read_config_32(mport, destid, hopcount, RIO_SRC_OPS_CAR,
  1506. &rdev->src_ops);
  1507. rio_mport_read_config_32(mport, destid, hopcount, RIO_DST_OPS_CAR,
  1508. &rdev->dst_ops);
  1509. rdev->comp_tag = dev_info.comptag;
  1510. rdev->destid = destid;
  1511. /* hopcount is stored as specified by a caller, regardles of EP or SW */
  1512. rdev->hopcount = hopcount;
  1513. if (rdev->pef & RIO_PEF_SWITCH) {
  1514. rswitch = rdev->rswitch;
  1515. rswitch->route_table = NULL;
  1516. }
  1517. if (strlen(dev_info.name))
  1518. dev_set_name(&rdev->dev, "%s", dev_info.name);
  1519. else if (rdev->pef & RIO_PEF_SWITCH)
  1520. dev_set_name(&rdev->dev, "%02x:s:%04x", mport->id,
  1521. rdev->comp_tag & RIO_CTAG_UDEVID);
  1522. else
  1523. dev_set_name(&rdev->dev, "%02x:e:%04x", mport->id,
  1524. rdev->comp_tag & RIO_CTAG_UDEVID);
  1525. INIT_LIST_HEAD(&rdev->net_list);
  1526. rdev->dev.parent = &mport->net->dev;
  1527. rio_attach_device(rdev);
  1528. rdev->dev.release = rio_release_dev;
  1529. if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
  1530. rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
  1531. 0, 0xffff);
  1532. err = rio_add_device(rdev);
  1533. if (err)
  1534. goto cleanup;
  1535. rio_dev_get(rdev);
  1536. return 0;
  1537. cleanup:
  1538. kfree(rdev);
  1539. return err;
  1540. }
  1541. static int rio_mport_del_riodev(struct mport_cdev_priv *priv, void __user *arg)
  1542. {
  1543. struct rio_rdev_info dev_info;
  1544. struct rio_dev *rdev = NULL;
  1545. struct device *dev;
  1546. struct rio_mport *mport;
  1547. struct rio_net *net;
  1548. if (copy_from_user(&dev_info, arg, sizeof(dev_info)))
  1549. return -EFAULT;
  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);