rio_mport_cdev.c 67 KB

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