rio_mport_cdev.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734
  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. down_read(&current->mm->mmap_sem);
  770. pinned = get_user_pages(
  771. (unsigned long)xfer->loc_addr & PAGE_MASK,
  772. nr_pages,
  773. dir == DMA_FROM_DEVICE ? FOLL_WRITE : 0,
  774. page_list, NULL);
  775. up_read(&current->mm->mmap_sem);
  776. if (pinned != nr_pages) {
  777. if (pinned < 0) {
  778. rmcd_error("get_user_pages err=%ld", pinned);
  779. nr_pages = 0;
  780. } else
  781. rmcd_error("pinned %ld out of %ld pages",
  782. pinned, nr_pages);
  783. ret = -EFAULT;
  784. goto err_pg;
  785. }
  786. ret = sg_alloc_table_from_pages(&req->sgt, page_list, nr_pages,
  787. offset, xfer->length, GFP_KERNEL);
  788. if (ret) {
  789. rmcd_error("sg_alloc_table failed with err=%d", ret);
  790. goto err_pg;
  791. }
  792. req->page_list = page_list;
  793. req->nr_pages = nr_pages;
  794. } else {
  795. dma_addr_t baddr;
  796. struct rio_mport_mapping *map;
  797. baddr = (dma_addr_t)xfer->handle;
  798. mutex_lock(&md->buf_mutex);
  799. list_for_each_entry(map, &md->mappings, node) {
  800. if (baddr >= map->phys_addr &&
  801. baddr < (map->phys_addr + map->size)) {
  802. kref_get(&map->ref);
  803. req->map = map;
  804. break;
  805. }
  806. }
  807. mutex_unlock(&md->buf_mutex);
  808. if (req->map == NULL) {
  809. ret = -ENOMEM;
  810. goto err_req;
  811. }
  812. if (xfer->length + xfer->offset > map->size) {
  813. ret = -EINVAL;
  814. goto err_req;
  815. }
  816. ret = sg_alloc_table(&req->sgt, 1, GFP_KERNEL);
  817. if (unlikely(ret)) {
  818. rmcd_error("sg_alloc_table failed for internal buf");
  819. goto err_req;
  820. }
  821. sg_set_buf(req->sgt.sgl,
  822. map->virt_addr + (baddr - map->phys_addr) +
  823. xfer->offset, xfer->length);
  824. }
  825. req->dir = dir;
  826. req->filp = filp;
  827. req->priv = priv;
  828. chan = priv->dmach;
  829. nents = dma_map_sg(chan->device->dev,
  830. req->sgt.sgl, req->sgt.nents, dir);
  831. if (nents == -EFAULT) {
  832. rmcd_error("Failed to map SG list");
  833. return -EFAULT;
  834. }
  835. ret = do_dma_request(req, xfer, sync, nents);
  836. if (ret >= 0) {
  837. if (sync == RIO_TRANSFER_SYNC)
  838. goto sync_out;
  839. return ret; /* return ASYNC cookie */
  840. }
  841. if (ret == -ETIMEDOUT || ret == -EINTR) {
  842. /*
  843. * This can happen only in case of SYNC transfer.
  844. * Do not free unfinished request structure immediately.
  845. * Place it into pending list and deal with it later
  846. */
  847. spin_lock(&priv->req_lock);
  848. list_add_tail(&req->node, &priv->pend_list);
  849. spin_unlock(&priv->req_lock);
  850. return ret;
  851. }
  852. rmcd_debug(DMA, "do_dma_request failed with err=%d", ret);
  853. sync_out:
  854. dma_unmap_sg(chan->device->dev, req->sgt.sgl, req->sgt.nents, dir);
  855. sg_free_table(&req->sgt);
  856. err_pg:
  857. if (page_list) {
  858. for (i = 0; i < nr_pages; i++)
  859. put_page(page_list[i]);
  860. kfree(page_list);
  861. }
  862. err_req:
  863. if (req->map) {
  864. mutex_lock(&md->buf_mutex);
  865. kref_put(&req->map->ref, mport_release_mapping);
  866. mutex_unlock(&md->buf_mutex);
  867. }
  868. put_dma_channel(priv);
  869. kfree(req);
  870. return ret;
  871. }
  872. static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg)
  873. {
  874. struct mport_cdev_priv *priv = filp->private_data;
  875. struct rio_transaction transaction;
  876. struct rio_transfer_io *transfer;
  877. enum dma_data_direction dir;
  878. int i, ret = 0;
  879. if (unlikely(copy_from_user(&transaction, arg, sizeof(transaction))))
  880. return -EFAULT;
  881. if (transaction.count != 1) /* only single transfer for now */
  882. return -EINVAL;
  883. if ((transaction.transfer_mode &
  884. priv->md->properties.transfer_mode) == 0)
  885. return -ENODEV;
  886. transfer = vmalloc(transaction.count * sizeof(*transfer));
  887. if (!transfer)
  888. return -ENOMEM;
  889. if (unlikely(copy_from_user(transfer,
  890. (void __user *)(uintptr_t)transaction.block,
  891. transaction.count * sizeof(*transfer)))) {
  892. ret = -EFAULT;
  893. goto out_free;
  894. }
  895. dir = (transaction.dir == RIO_TRANSFER_DIR_READ) ?
  896. DMA_FROM_DEVICE : DMA_TO_DEVICE;
  897. for (i = 0; i < transaction.count && ret == 0; i++)
  898. ret = rio_dma_transfer(filp, transaction.transfer_mode,
  899. transaction.sync, dir, &transfer[i]);
  900. if (unlikely(copy_to_user((void __user *)(uintptr_t)transaction.block,
  901. transfer,
  902. transaction.count * sizeof(*transfer))))
  903. ret = -EFAULT;
  904. out_free:
  905. vfree(transfer);
  906. return ret;
  907. }
  908. static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg)
  909. {
  910. struct mport_cdev_priv *priv;
  911. struct mport_dev *md;
  912. struct rio_async_tx_wait w_param;
  913. struct mport_dma_req *req;
  914. dma_cookie_t cookie;
  915. unsigned long tmo;
  916. long wret;
  917. int found = 0;
  918. int ret;
  919. priv = (struct mport_cdev_priv *)filp->private_data;
  920. md = priv->md;
  921. if (unlikely(copy_from_user(&w_param, arg, sizeof(w_param))))
  922. return -EFAULT;
  923. cookie = w_param.token;
  924. if (w_param.timeout)
  925. tmo = msecs_to_jiffies(w_param.timeout);
  926. else /* Use default DMA timeout */
  927. tmo = msecs_to_jiffies(dma_timeout);
  928. spin_lock(&priv->req_lock);
  929. list_for_each_entry(req, &priv->async_list, node) {
  930. if (req->cookie == cookie) {
  931. list_del(&req->node);
  932. found = 1;
  933. break;
  934. }
  935. }
  936. spin_unlock(&priv->req_lock);
  937. if (!found)
  938. return -EAGAIN;
  939. wret = wait_for_completion_interruptible_timeout(&req->req_comp, tmo);
  940. if (wret == 0) {
  941. /* Timeout on wait occurred */
  942. rmcd_error("%s(%d) timed out waiting for ASYNC DMA_%s",
  943. current->comm, task_pid_nr(current),
  944. (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE");
  945. ret = -ETIMEDOUT;
  946. goto err_tmo;
  947. } else if (wret == -ERESTARTSYS) {
  948. /* Wait_for_completion was interrupted by a signal but DMA may
  949. * be still in progress
  950. */
  951. rmcd_error("%s(%d) wait for ASYNC DMA_%s was interrupted",
  952. current->comm, task_pid_nr(current),
  953. (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE");
  954. ret = -EINTR;
  955. goto err_tmo;
  956. }
  957. if (req->status != DMA_COMPLETE) {
  958. /* DMA transaction completion signaled with transfer error */
  959. rmcd_error("%s(%d) ASYNC DMA_%s completion with status %d",
  960. current->comm, task_pid_nr(current),
  961. (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE",
  962. req->status);
  963. ret = -EIO;
  964. } else
  965. ret = 0;
  966. if (req->status != DMA_IN_PROGRESS && req->status != DMA_PAUSED)
  967. dma_req_free(req);
  968. return ret;
  969. err_tmo:
  970. /* Return request back into async queue */
  971. spin_lock(&priv->req_lock);
  972. list_add_tail(&req->node, &priv->async_list);
  973. spin_unlock(&priv->req_lock);
  974. return ret;
  975. }
  976. static int rio_mport_create_dma_mapping(struct mport_dev *md, struct file *filp,
  977. u64 size, struct rio_mport_mapping **mapping)
  978. {
  979. struct rio_mport_mapping *map;
  980. map = kzalloc(sizeof(*map), GFP_KERNEL);
  981. if (map == NULL)
  982. return -ENOMEM;
  983. map->virt_addr = dma_alloc_coherent(md->mport->dev.parent, size,
  984. &map->phys_addr, GFP_KERNEL);
  985. if (map->virt_addr == NULL) {
  986. kfree(map);
  987. return -ENOMEM;
  988. }
  989. map->dir = MAP_DMA;
  990. map->size = size;
  991. map->filp = filp;
  992. map->md = md;
  993. kref_init(&map->ref);
  994. mutex_lock(&md->buf_mutex);
  995. list_add_tail(&map->node, &md->mappings);
  996. mutex_unlock(&md->buf_mutex);
  997. *mapping = map;
  998. return 0;
  999. }
  1000. static int rio_mport_alloc_dma(struct file *filp, void __user *arg)
  1001. {
  1002. struct mport_cdev_priv *priv = filp->private_data;
  1003. struct mport_dev *md = priv->md;
  1004. struct rio_dma_mem map;
  1005. struct rio_mport_mapping *mapping = NULL;
  1006. int ret;
  1007. if (unlikely(copy_from_user(&map, arg, sizeof(map))))
  1008. return -EFAULT;
  1009. ret = rio_mport_create_dma_mapping(md, filp, map.length, &mapping);
  1010. if (ret)
  1011. return ret;
  1012. map.dma_handle = mapping->phys_addr;
  1013. if (unlikely(copy_to_user(arg, &map, sizeof(map)))) {
  1014. mutex_lock(&md->buf_mutex);
  1015. kref_put(&mapping->ref, mport_release_mapping);
  1016. mutex_unlock(&md->buf_mutex);
  1017. return -EFAULT;
  1018. }
  1019. return 0;
  1020. }
  1021. static int rio_mport_free_dma(struct file *filp, void __user *arg)
  1022. {
  1023. struct mport_cdev_priv *priv = filp->private_data;
  1024. struct mport_dev *md = priv->md;
  1025. u64 handle;
  1026. int ret = -EFAULT;
  1027. struct rio_mport_mapping *map, *_map;
  1028. if (copy_from_user(&handle, arg, sizeof(handle)))
  1029. return -EFAULT;
  1030. rmcd_debug(EXIT, "filp=%p", filp);
  1031. mutex_lock(&md->buf_mutex);
  1032. list_for_each_entry_safe(map, _map, &md->mappings, node) {
  1033. if (map->dir == MAP_DMA && map->phys_addr == handle &&
  1034. map->filp == filp) {
  1035. kref_put(&map->ref, mport_release_mapping);
  1036. ret = 0;
  1037. break;
  1038. }
  1039. }
  1040. mutex_unlock(&md->buf_mutex);
  1041. if (ret == -EFAULT) {
  1042. rmcd_debug(DMA, "ERR no matching mapping");
  1043. return ret;
  1044. }
  1045. return 0;
  1046. }
  1047. #else
  1048. static int rio_mport_transfer_ioctl(struct file *filp, void *arg)
  1049. {
  1050. return -ENODEV;
  1051. }
  1052. static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg)
  1053. {
  1054. return -ENODEV;
  1055. }
  1056. static int rio_mport_alloc_dma(struct file *filp, void __user *arg)
  1057. {
  1058. return -ENODEV;
  1059. }
  1060. static int rio_mport_free_dma(struct file *filp, void __user *arg)
  1061. {
  1062. return -ENODEV;
  1063. }
  1064. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  1065. /*
  1066. * Inbound/outbound memory mapping functions
  1067. */
  1068. static int
  1069. rio_mport_create_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 *mport = md->mport;
  1074. struct rio_mport_mapping *map;
  1075. int ret;
  1076. /* rio_map_inb_region() accepts u32 size */
  1077. if (size > 0xffffffff)
  1078. return -EINVAL;
  1079. map = kzalloc(sizeof(*map), GFP_KERNEL);
  1080. if (map == NULL)
  1081. return -ENOMEM;
  1082. map->virt_addr = dma_alloc_coherent(mport->dev.parent, size,
  1083. &map->phys_addr, GFP_KERNEL);
  1084. if (map->virt_addr == NULL) {
  1085. ret = -ENOMEM;
  1086. goto err_dma_alloc;
  1087. }
  1088. if (raddr == RIO_MAP_ANY_ADDR)
  1089. raddr = map->phys_addr;
  1090. ret = rio_map_inb_region(mport, map->phys_addr, raddr, (u32)size, 0);
  1091. if (ret < 0)
  1092. goto err_map_inb;
  1093. map->dir = MAP_INBOUND;
  1094. map->rio_addr = raddr;
  1095. map->size = size;
  1096. map->filp = filp;
  1097. map->md = md;
  1098. kref_init(&map->ref);
  1099. mutex_lock(&md->buf_mutex);
  1100. list_add_tail(&map->node, &md->mappings);
  1101. mutex_unlock(&md->buf_mutex);
  1102. *mapping = map;
  1103. return 0;
  1104. err_map_inb:
  1105. dma_free_coherent(mport->dev.parent, size,
  1106. map->virt_addr, map->phys_addr);
  1107. err_dma_alloc:
  1108. kfree(map);
  1109. return ret;
  1110. }
  1111. static int
  1112. rio_mport_get_inbound_mapping(struct mport_dev *md, struct file *filp,
  1113. u64 raddr, u64 size,
  1114. struct rio_mport_mapping **mapping)
  1115. {
  1116. struct rio_mport_mapping *map;
  1117. int err = -ENOMEM;
  1118. if (raddr == RIO_MAP_ANY_ADDR)
  1119. goto get_new;
  1120. mutex_lock(&md->buf_mutex);
  1121. list_for_each_entry(map, &md->mappings, node) {
  1122. if (map->dir != MAP_INBOUND)
  1123. continue;
  1124. if (raddr == map->rio_addr && size == map->size) {
  1125. /* allow exact match only */
  1126. *mapping = map;
  1127. err = 0;
  1128. break;
  1129. } else if (raddr < (map->rio_addr + map->size - 1) &&
  1130. (raddr + size) > map->rio_addr) {
  1131. err = -EBUSY;
  1132. break;
  1133. }
  1134. }
  1135. mutex_unlock(&md->buf_mutex);
  1136. if (err != -ENOMEM)
  1137. return err;
  1138. get_new:
  1139. /* not found, create new */
  1140. return rio_mport_create_inbound_mapping(md, filp, raddr, size, mapping);
  1141. }
  1142. static int rio_mport_map_inbound(struct file *filp, void __user *arg)
  1143. {
  1144. struct mport_cdev_priv *priv = filp->private_data;
  1145. struct mport_dev *md = priv->md;
  1146. struct rio_mmap map;
  1147. struct rio_mport_mapping *mapping = NULL;
  1148. int ret;
  1149. if (!md->mport->ops->map_inb)
  1150. return -EPROTONOSUPPORT;
  1151. if (unlikely(copy_from_user(&map, arg, sizeof(map))))
  1152. return -EFAULT;
  1153. rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp);
  1154. ret = rio_mport_get_inbound_mapping(md, filp, map.rio_addr,
  1155. map.length, &mapping);
  1156. if (ret)
  1157. return ret;
  1158. map.handle = mapping->phys_addr;
  1159. map.rio_addr = mapping->rio_addr;
  1160. if (unlikely(copy_to_user(arg, &map, sizeof(map)))) {
  1161. /* Delete mapping if it was created by this request */
  1162. if (ret == 0 && mapping->filp == filp) {
  1163. mutex_lock(&md->buf_mutex);
  1164. kref_put(&mapping->ref, mport_release_mapping);
  1165. mutex_unlock(&md->buf_mutex);
  1166. }
  1167. return -EFAULT;
  1168. }
  1169. return 0;
  1170. }
  1171. /*
  1172. * rio_mport_inbound_free() - unmap from RapidIO address space and free
  1173. * previously allocated inbound DMA coherent buffer
  1174. * @priv: driver private data
  1175. * @arg: buffer handle returned by allocation routine
  1176. */
  1177. static int rio_mport_inbound_free(struct file *filp, void __user *arg)
  1178. {
  1179. struct mport_cdev_priv *priv = filp->private_data;
  1180. struct mport_dev *md = priv->md;
  1181. u64 handle;
  1182. struct rio_mport_mapping *map, *_map;
  1183. rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp);
  1184. if (!md->mport->ops->unmap_inb)
  1185. return -EPROTONOSUPPORT;
  1186. if (copy_from_user(&handle, arg, sizeof(handle)))
  1187. return -EFAULT;
  1188. mutex_lock(&md->buf_mutex);
  1189. list_for_each_entry_safe(map, _map, &md->mappings, node) {
  1190. if (map->dir == MAP_INBOUND && map->phys_addr == handle) {
  1191. if (map->filp == filp) {
  1192. map->filp = NULL;
  1193. kref_put(&map->ref, mport_release_mapping);
  1194. }
  1195. break;
  1196. }
  1197. }
  1198. mutex_unlock(&md->buf_mutex);
  1199. return 0;
  1200. }
  1201. /*
  1202. * maint_port_idx_get() - Get the port index of the mport instance
  1203. * @priv: driver private data
  1204. * @arg: port index
  1205. */
  1206. static int maint_port_idx_get(struct mport_cdev_priv *priv, void __user *arg)
  1207. {
  1208. struct mport_dev *md = priv->md;
  1209. u32 port_idx = md->mport->index;
  1210. rmcd_debug(MPORT, "port_index=%d", port_idx);
  1211. if (copy_to_user(arg, &port_idx, sizeof(port_idx)))
  1212. return -EFAULT;
  1213. return 0;
  1214. }
  1215. static int rio_mport_add_event(struct mport_cdev_priv *priv,
  1216. struct rio_event *event)
  1217. {
  1218. int overflow;
  1219. if (!(priv->event_mask & event->header))
  1220. return -EACCES;
  1221. spin_lock(&priv->fifo_lock);
  1222. overflow = kfifo_avail(&priv->event_fifo) < sizeof(*event)
  1223. || kfifo_in(&priv->event_fifo, (unsigned char *)event,
  1224. sizeof(*event)) != sizeof(*event);
  1225. spin_unlock(&priv->fifo_lock);
  1226. wake_up_interruptible(&priv->event_rx_wait);
  1227. if (overflow) {
  1228. dev_warn(&priv->md->dev, DRV_NAME ": event fifo overflow\n");
  1229. return -EBUSY;
  1230. }
  1231. return 0;
  1232. }
  1233. static void rio_mport_doorbell_handler(struct rio_mport *mport, void *dev_id,
  1234. u16 src, u16 dst, u16 info)
  1235. {
  1236. struct mport_dev *data = dev_id;
  1237. struct mport_cdev_priv *priv;
  1238. struct rio_mport_db_filter *db_filter;
  1239. struct rio_event event;
  1240. int handled;
  1241. event.header = RIO_DOORBELL;
  1242. event.u.doorbell.rioid = src;
  1243. event.u.doorbell.payload = info;
  1244. handled = 0;
  1245. spin_lock(&data->db_lock);
  1246. list_for_each_entry(db_filter, &data->doorbells, data_node) {
  1247. if (((db_filter->filter.rioid == RIO_INVALID_DESTID ||
  1248. db_filter->filter.rioid == src)) &&
  1249. info >= db_filter->filter.low &&
  1250. info <= db_filter->filter.high) {
  1251. priv = db_filter->priv;
  1252. rio_mport_add_event(priv, &event);
  1253. handled = 1;
  1254. }
  1255. }
  1256. spin_unlock(&data->db_lock);
  1257. if (!handled)
  1258. dev_warn(&data->dev,
  1259. "%s: spurious DB received from 0x%x, info=0x%04x\n",
  1260. __func__, src, info);
  1261. }
  1262. static int rio_mport_add_db_filter(struct mport_cdev_priv *priv,
  1263. void __user *arg)
  1264. {
  1265. struct mport_dev *md = priv->md;
  1266. struct rio_mport_db_filter *db_filter;
  1267. struct rio_doorbell_filter filter;
  1268. unsigned long flags;
  1269. int ret;
  1270. if (copy_from_user(&filter, arg, sizeof(filter)))
  1271. return -EFAULT;
  1272. if (filter.low > filter.high)
  1273. return -EINVAL;
  1274. ret = rio_request_inb_dbell(md->mport, md, filter.low, filter.high,
  1275. rio_mport_doorbell_handler);
  1276. if (ret) {
  1277. rmcd_error("%s failed to register IBDB, err=%d",
  1278. dev_name(&md->dev), ret);
  1279. return ret;
  1280. }
  1281. db_filter = kzalloc(sizeof(*db_filter), GFP_KERNEL);
  1282. if (db_filter == NULL) {
  1283. rio_release_inb_dbell(md->mport, filter.low, filter.high);
  1284. return -ENOMEM;
  1285. }
  1286. db_filter->filter = filter;
  1287. db_filter->priv = priv;
  1288. spin_lock_irqsave(&md->db_lock, flags);
  1289. list_add_tail(&db_filter->priv_node, &priv->db_filters);
  1290. list_add_tail(&db_filter->data_node, &md->doorbells);
  1291. spin_unlock_irqrestore(&md->db_lock, flags);
  1292. return 0;
  1293. }
  1294. static void rio_mport_delete_db_filter(struct rio_mport_db_filter *db_filter)
  1295. {
  1296. list_del(&db_filter->data_node);
  1297. list_del(&db_filter->priv_node);
  1298. kfree(db_filter);
  1299. }
  1300. static int rio_mport_remove_db_filter(struct mport_cdev_priv *priv,
  1301. void __user *arg)
  1302. {
  1303. struct rio_mport_db_filter *db_filter;
  1304. struct rio_doorbell_filter filter;
  1305. unsigned long flags;
  1306. int ret = -EINVAL;
  1307. if (copy_from_user(&filter, arg, sizeof(filter)))
  1308. return -EFAULT;
  1309. if (filter.low > filter.high)
  1310. return -EINVAL;
  1311. spin_lock_irqsave(&priv->md->db_lock, flags);
  1312. list_for_each_entry(db_filter, &priv->db_filters, priv_node) {
  1313. if (db_filter->filter.rioid == filter.rioid &&
  1314. db_filter->filter.low == filter.low &&
  1315. db_filter->filter.high == filter.high) {
  1316. rio_mport_delete_db_filter(db_filter);
  1317. ret = 0;
  1318. break;
  1319. }
  1320. }
  1321. spin_unlock_irqrestore(&priv->md->db_lock, flags);
  1322. if (!ret)
  1323. rio_release_inb_dbell(priv->md->mport, filter.low, filter.high);
  1324. return ret;
  1325. }
  1326. static int rio_mport_match_pw(union rio_pw_msg *msg,
  1327. struct rio_pw_filter *filter)
  1328. {
  1329. if ((msg->em.comptag & filter->mask) < filter->low ||
  1330. (msg->em.comptag & filter->mask) > filter->high)
  1331. return 0;
  1332. return 1;
  1333. }
  1334. static int rio_mport_pw_handler(struct rio_mport *mport, void *context,
  1335. union rio_pw_msg *msg, int step)
  1336. {
  1337. struct mport_dev *md = context;
  1338. struct mport_cdev_priv *priv;
  1339. struct rio_mport_pw_filter *pw_filter;
  1340. struct rio_event event;
  1341. int handled;
  1342. event.header = RIO_PORTWRITE;
  1343. memcpy(event.u.portwrite.payload, msg->raw, RIO_PW_MSG_SIZE);
  1344. handled = 0;
  1345. spin_lock(&md->pw_lock);
  1346. list_for_each_entry(pw_filter, &md->portwrites, md_node) {
  1347. if (rio_mport_match_pw(msg, &pw_filter->filter)) {
  1348. priv = pw_filter->priv;
  1349. rio_mport_add_event(priv, &event);
  1350. handled = 1;
  1351. }
  1352. }
  1353. spin_unlock(&md->pw_lock);
  1354. if (!handled) {
  1355. printk_ratelimited(KERN_WARNING DRV_NAME
  1356. ": mport%d received spurious PW from 0x%08x\n",
  1357. mport->id, msg->em.comptag);
  1358. }
  1359. return 0;
  1360. }
  1361. static int rio_mport_add_pw_filter(struct mport_cdev_priv *priv,
  1362. void __user *arg)
  1363. {
  1364. struct mport_dev *md = priv->md;
  1365. struct rio_mport_pw_filter *pw_filter;
  1366. struct rio_pw_filter filter;
  1367. unsigned long flags;
  1368. int hadd = 0;
  1369. if (copy_from_user(&filter, arg, sizeof(filter)))
  1370. return -EFAULT;
  1371. pw_filter = kzalloc(sizeof(*pw_filter), GFP_KERNEL);
  1372. if (pw_filter == NULL)
  1373. return -ENOMEM;
  1374. pw_filter->filter = filter;
  1375. pw_filter->priv = priv;
  1376. spin_lock_irqsave(&md->pw_lock, flags);
  1377. if (list_empty(&md->portwrites))
  1378. hadd = 1;
  1379. list_add_tail(&pw_filter->priv_node, &priv->pw_filters);
  1380. list_add_tail(&pw_filter->md_node, &md->portwrites);
  1381. spin_unlock_irqrestore(&md->pw_lock, flags);
  1382. if (hadd) {
  1383. int ret;
  1384. ret = rio_add_mport_pw_handler(md->mport, md,
  1385. rio_mport_pw_handler);
  1386. if (ret) {
  1387. dev_err(&md->dev,
  1388. "%s: failed to add IB_PW handler, err=%d\n",
  1389. __func__, ret);
  1390. return ret;
  1391. }
  1392. rio_pw_enable(md->mport, 1);
  1393. }
  1394. return 0;
  1395. }
  1396. static void rio_mport_delete_pw_filter(struct rio_mport_pw_filter *pw_filter)
  1397. {
  1398. list_del(&pw_filter->md_node);
  1399. list_del(&pw_filter->priv_node);
  1400. kfree(pw_filter);
  1401. }
  1402. static int rio_mport_match_pw_filter(struct rio_pw_filter *a,
  1403. struct rio_pw_filter *b)
  1404. {
  1405. if ((a->mask == b->mask) && (a->low == b->low) && (a->high == b->high))
  1406. return 1;
  1407. return 0;
  1408. }
  1409. static int rio_mport_remove_pw_filter(struct mport_cdev_priv *priv,
  1410. void __user *arg)
  1411. {
  1412. struct mport_dev *md = priv->md;
  1413. struct rio_mport_pw_filter *pw_filter;
  1414. struct rio_pw_filter filter;
  1415. unsigned long flags;
  1416. int ret = -EINVAL;
  1417. int hdel = 0;
  1418. if (copy_from_user(&filter, arg, sizeof(filter)))
  1419. return -EFAULT;
  1420. spin_lock_irqsave(&md->pw_lock, flags);
  1421. list_for_each_entry(pw_filter, &priv->pw_filters, priv_node) {
  1422. if (rio_mport_match_pw_filter(&pw_filter->filter, &filter)) {
  1423. rio_mport_delete_pw_filter(pw_filter);
  1424. ret = 0;
  1425. break;
  1426. }
  1427. }
  1428. if (list_empty(&md->portwrites))
  1429. hdel = 1;
  1430. spin_unlock_irqrestore(&md->pw_lock, flags);
  1431. if (hdel) {
  1432. rio_del_mport_pw_handler(md->mport, priv->md,
  1433. rio_mport_pw_handler);
  1434. rio_pw_enable(md->mport, 0);
  1435. }
  1436. return ret;
  1437. }
  1438. /*
  1439. * rio_release_dev - release routine for kernel RIO device object
  1440. * @dev: kernel device object associated with a RIO device structure
  1441. *
  1442. * Frees a RIO device struct associated a RIO device struct.
  1443. * The RIO device struct is freed.
  1444. */
  1445. static void rio_release_dev(struct device *dev)
  1446. {
  1447. struct rio_dev *rdev;
  1448. rdev = to_rio_dev(dev);
  1449. pr_info(DRV_PREFIX "%s: %s\n", __func__, rio_name(rdev));
  1450. kfree(rdev);
  1451. }
  1452. static void rio_release_net(struct device *dev)
  1453. {
  1454. struct rio_net *net;
  1455. net = to_rio_net(dev);
  1456. rmcd_debug(RDEV, "net_%d", net->id);
  1457. kfree(net);
  1458. }
  1459. /*
  1460. * rio_mport_add_riodev - creates a kernel RIO device object
  1461. *
  1462. * Allocates a RIO device data structure and initializes required fields based
  1463. * on device's configuration space contents.
  1464. * If the device has switch capabilities, then a switch specific portion is
  1465. * allocated and configured.
  1466. */
  1467. static int rio_mport_add_riodev(struct mport_cdev_priv *priv,
  1468. void __user *arg)
  1469. {
  1470. struct mport_dev *md = priv->md;
  1471. struct rio_rdev_info dev_info;
  1472. struct rio_dev *rdev;
  1473. struct rio_switch *rswitch = NULL;
  1474. struct rio_mport *mport;
  1475. size_t size;
  1476. u32 rval;
  1477. u32 swpinfo = 0;
  1478. u16 destid;
  1479. u8 hopcount;
  1480. int err;
  1481. if (copy_from_user(&dev_info, arg, sizeof(dev_info)))
  1482. return -EFAULT;
  1483. rmcd_debug(RDEV, "name:%s ct:0x%x did:0x%x hc:0x%x", dev_info.name,
  1484. dev_info.comptag, dev_info.destid, dev_info.hopcount);
  1485. if (bus_find_device_by_name(&rio_bus_type, NULL, dev_info.name)) {
  1486. rmcd_debug(RDEV, "device %s already exists", dev_info.name);
  1487. return -EEXIST;
  1488. }
  1489. size = sizeof(*rdev);
  1490. mport = md->mport;
  1491. destid = dev_info.destid;
  1492. hopcount = dev_info.hopcount;
  1493. if (rio_mport_read_config_32(mport, destid, hopcount,
  1494. RIO_PEF_CAR, &rval))
  1495. return -EIO;
  1496. if (rval & RIO_PEF_SWITCH) {
  1497. rio_mport_read_config_32(mport, destid, hopcount,
  1498. RIO_SWP_INFO_CAR, &swpinfo);
  1499. size += (RIO_GET_TOTAL_PORTS(swpinfo) *
  1500. sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
  1501. }
  1502. rdev = kzalloc(size, GFP_KERNEL);
  1503. if (rdev == NULL)
  1504. return -ENOMEM;
  1505. if (mport->net == NULL) {
  1506. struct rio_net *net;
  1507. net = rio_alloc_net(mport);
  1508. if (!net) {
  1509. err = -ENOMEM;
  1510. rmcd_debug(RDEV, "failed to allocate net object");
  1511. goto cleanup;
  1512. }
  1513. net->id = mport->id;
  1514. net->hport = mport;
  1515. dev_set_name(&net->dev, "rnet_%d", net->id);
  1516. net->dev.parent = &mport->dev;
  1517. net->dev.release = rio_release_net;
  1518. err = rio_add_net(net);
  1519. if (err) {
  1520. rmcd_debug(RDEV, "failed to register net, err=%d", err);
  1521. kfree(net);
  1522. goto cleanup;
  1523. }
  1524. }
  1525. rdev->net = mport->net;
  1526. rdev->pef = rval;
  1527. rdev->swpinfo = swpinfo;
  1528. rio_mport_read_config_32(mport, destid, hopcount,
  1529. RIO_DEV_ID_CAR, &rval);
  1530. rdev->did = rval >> 16;
  1531. rdev->vid = rval & 0xffff;
  1532. rio_mport_read_config_32(mport, destid, hopcount, RIO_DEV_INFO_CAR,
  1533. &rdev->device_rev);
  1534. rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_ID_CAR,
  1535. &rval);
  1536. rdev->asm_did = rval >> 16;
  1537. rdev->asm_vid = rval & 0xffff;
  1538. rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_INFO_CAR,
  1539. &rval);
  1540. rdev->asm_rev = rval >> 16;
  1541. if (rdev->pef & RIO_PEF_EXT_FEATURES) {
  1542. rdev->efptr = rval & 0xffff;
  1543. rdev->phys_efptr = rio_mport_get_physefb(mport, 0, destid,
  1544. hopcount, &rdev->phys_rmap);
  1545. rdev->em_efptr = rio_mport_get_feature(mport, 0, destid,
  1546. hopcount, RIO_EFB_ERR_MGMNT);
  1547. }
  1548. rio_mport_read_config_32(mport, destid, hopcount, RIO_SRC_OPS_CAR,
  1549. &rdev->src_ops);
  1550. rio_mport_read_config_32(mport, destid, hopcount, RIO_DST_OPS_CAR,
  1551. &rdev->dst_ops);
  1552. rdev->comp_tag = dev_info.comptag;
  1553. rdev->destid = destid;
  1554. /* hopcount is stored as specified by a caller, regardles of EP or SW */
  1555. rdev->hopcount = hopcount;
  1556. if (rdev->pef & RIO_PEF_SWITCH) {
  1557. rswitch = rdev->rswitch;
  1558. rswitch->route_table = NULL;
  1559. }
  1560. if (strlen(dev_info.name))
  1561. dev_set_name(&rdev->dev, "%s", dev_info.name);
  1562. else if (rdev->pef & RIO_PEF_SWITCH)
  1563. dev_set_name(&rdev->dev, "%02x:s:%04x", mport->id,
  1564. rdev->comp_tag & RIO_CTAG_UDEVID);
  1565. else
  1566. dev_set_name(&rdev->dev, "%02x:e:%04x", mport->id,
  1567. rdev->comp_tag & RIO_CTAG_UDEVID);
  1568. INIT_LIST_HEAD(&rdev->net_list);
  1569. rdev->dev.parent = &mport->net->dev;
  1570. rio_attach_device(rdev);
  1571. rdev->dev.release = rio_release_dev;
  1572. if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
  1573. rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
  1574. 0, 0xffff);
  1575. err = rio_add_device(rdev);
  1576. if (err)
  1577. goto cleanup;
  1578. rio_dev_get(rdev);
  1579. return 0;
  1580. cleanup:
  1581. kfree(rdev);
  1582. return err;
  1583. }
  1584. static int rio_mport_del_riodev(struct mport_cdev_priv *priv, void __user *arg)
  1585. {
  1586. struct rio_rdev_info dev_info;
  1587. struct rio_dev *rdev = NULL;
  1588. struct device *dev;
  1589. struct rio_mport *mport;
  1590. struct rio_net *net;
  1591. if (copy_from_user(&dev_info, arg, sizeof(dev_info)))
  1592. return -EFAULT;
  1593. mport = priv->md->mport;
  1594. /* If device name is specified, removal by name has priority */
  1595. if (strlen(dev_info.name)) {
  1596. dev = bus_find_device_by_name(&rio_bus_type, NULL,
  1597. dev_info.name);
  1598. if (dev)
  1599. rdev = to_rio_dev(dev);
  1600. } else {
  1601. do {
  1602. rdev = rio_get_comptag(dev_info.comptag, rdev);
  1603. if (rdev && rdev->dev.parent == &mport->net->dev &&
  1604. rdev->destid == dev_info.destid &&
  1605. rdev->hopcount == dev_info.hopcount)
  1606. break;
  1607. } while (rdev);
  1608. }
  1609. if (!rdev) {
  1610. rmcd_debug(RDEV,
  1611. "device name:%s ct:0x%x did:0x%x hc:0x%x not found",
  1612. dev_info.name, dev_info.comptag, dev_info.destid,
  1613. dev_info.hopcount);
  1614. return -ENODEV;
  1615. }
  1616. net = rdev->net;
  1617. rio_dev_put(rdev);
  1618. rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
  1619. if (list_empty(&net->devices)) {
  1620. rio_free_net(net);
  1621. mport->net = NULL;
  1622. }
  1623. return 0;
  1624. }
  1625. /*
  1626. * Mport cdev management
  1627. */
  1628. /*
  1629. * mport_cdev_open() - Open character device (mport)
  1630. */
  1631. static int mport_cdev_open(struct inode *inode, struct file *filp)
  1632. {
  1633. int ret;
  1634. int minor = iminor(inode);
  1635. struct mport_dev *chdev;
  1636. struct mport_cdev_priv *priv;
  1637. /* Test for valid device */
  1638. if (minor >= RIO_MAX_MPORTS) {
  1639. rmcd_error("Invalid minor device number");
  1640. return -EINVAL;
  1641. }
  1642. chdev = container_of(inode->i_cdev, struct mport_dev, cdev);
  1643. rmcd_debug(INIT, "%s filp=%p", dev_name(&chdev->dev), filp);
  1644. if (atomic_read(&chdev->active) == 0)
  1645. return -ENODEV;
  1646. get_device(&chdev->dev);
  1647. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1648. if (!priv) {
  1649. put_device(&chdev->dev);
  1650. return -ENOMEM;
  1651. }
  1652. priv->md = chdev;
  1653. mutex_lock(&chdev->file_mutex);
  1654. list_add_tail(&priv->list, &chdev->file_list);
  1655. mutex_unlock(&chdev->file_mutex);
  1656. INIT_LIST_HEAD(&priv->db_filters);
  1657. INIT_LIST_HEAD(&priv->pw_filters);
  1658. spin_lock_init(&priv->fifo_lock);
  1659. init_waitqueue_head(&priv->event_rx_wait);
  1660. ret = kfifo_alloc(&priv->event_fifo,
  1661. sizeof(struct rio_event) * MPORT_EVENT_DEPTH,
  1662. GFP_KERNEL);
  1663. if (ret < 0) {
  1664. dev_err(&chdev->dev, DRV_NAME ": kfifo_alloc failed\n");
  1665. ret = -ENOMEM;
  1666. goto err_fifo;
  1667. }
  1668. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  1669. INIT_LIST_HEAD(&priv->async_list);
  1670. INIT_LIST_HEAD(&priv->pend_list);
  1671. spin_lock_init(&priv->req_lock);
  1672. mutex_init(&priv->dma_lock);
  1673. #endif
  1674. filp->private_data = priv;
  1675. goto out;
  1676. err_fifo:
  1677. kfree(priv);
  1678. out:
  1679. return ret;
  1680. }
  1681. static int mport_cdev_fasync(int fd, struct file *filp, int mode)
  1682. {
  1683. struct mport_cdev_priv *priv = filp->private_data;
  1684. return fasync_helper(fd, filp, mode, &priv->async_queue);
  1685. }
  1686. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  1687. static void mport_cdev_release_dma(struct file *filp)
  1688. {
  1689. struct mport_cdev_priv *priv = filp->private_data;
  1690. struct mport_dev *md;
  1691. struct mport_dma_req *req, *req_next;
  1692. unsigned long tmo = msecs_to_jiffies(dma_timeout);
  1693. long wret;
  1694. LIST_HEAD(list);
  1695. rmcd_debug(EXIT, "from filp=%p %s(%d)",
  1696. filp, current->comm, task_pid_nr(current));
  1697. if (!priv->dmach) {
  1698. rmcd_debug(EXIT, "No DMA channel for filp=%p", filp);
  1699. return;
  1700. }
  1701. md = priv->md;
  1702. flush_workqueue(dma_wq);
  1703. spin_lock(&priv->req_lock);
  1704. if (!list_empty(&priv->async_list)) {
  1705. rmcd_debug(EXIT, "async list not empty filp=%p %s(%d)",
  1706. filp, current->comm, task_pid_nr(current));
  1707. list_splice_init(&priv->async_list, &list);
  1708. }
  1709. spin_unlock(&priv->req_lock);
  1710. if (!list_empty(&list)) {
  1711. rmcd_debug(EXIT, "temp list not empty");
  1712. list_for_each_entry_safe(req, req_next, &list, node) {
  1713. rmcd_debug(EXIT, "free req->filp=%p cookie=%d compl=%s",
  1714. req->filp, req->cookie,
  1715. completion_done(&req->req_comp)?"yes":"no");
  1716. list_del(&req->node);
  1717. dma_req_free(req);
  1718. }
  1719. }
  1720. if (!list_empty(&priv->pend_list)) {
  1721. rmcd_debug(EXIT, "Free pending DMA requests for filp=%p %s(%d)",
  1722. filp, current->comm, task_pid_nr(current));
  1723. list_for_each_entry_safe(req,
  1724. req_next, &priv->pend_list, node) {
  1725. rmcd_debug(EXIT, "free req->filp=%p cookie=%d compl=%s",
  1726. req->filp, req->cookie,
  1727. completion_done(&req->req_comp)?"yes":"no");
  1728. list_del(&req->node);
  1729. dma_req_free(req);
  1730. }
  1731. }
  1732. put_dma_channel(priv);
  1733. wret = wait_for_completion_interruptible_timeout(&priv->comp, tmo);
  1734. if (wret <= 0) {
  1735. rmcd_error("%s(%d) failed waiting for DMA release err=%ld",
  1736. current->comm, task_pid_nr(current), wret);
  1737. }
  1738. spin_lock(&priv->req_lock);
  1739. if (!list_empty(&priv->pend_list)) {
  1740. rmcd_debug(EXIT, "ATTN: pending DMA requests, filp=%p %s(%d)",
  1741. filp, current->comm, task_pid_nr(current));
  1742. }
  1743. spin_unlock(&priv->req_lock);
  1744. if (priv->dmach != priv->md->dma_chan) {
  1745. rmcd_debug(EXIT, "Release DMA channel for filp=%p %s(%d)",
  1746. filp, current->comm, task_pid_nr(current));
  1747. rio_release_dma(priv->dmach);
  1748. } else {
  1749. rmcd_debug(EXIT, "Adjust default DMA channel refcount");
  1750. kref_put(&md->dma_ref, mport_release_def_dma);
  1751. }
  1752. priv->dmach = NULL;
  1753. }
  1754. #else
  1755. #define mport_cdev_release_dma(priv) do {} while (0)
  1756. #endif
  1757. /*
  1758. * mport_cdev_release() - Release character device
  1759. */
  1760. static int mport_cdev_release(struct inode *inode, struct file *filp)
  1761. {
  1762. struct mport_cdev_priv *priv = filp->private_data;
  1763. struct mport_dev *chdev;
  1764. struct rio_mport_pw_filter *pw_filter, *pw_filter_next;
  1765. struct rio_mport_db_filter *db_filter, *db_filter_next;
  1766. struct rio_mport_mapping *map, *_map;
  1767. unsigned long flags;
  1768. rmcd_debug(EXIT, "%s filp=%p", dev_name(&priv->md->dev), filp);
  1769. chdev = priv->md;
  1770. mport_cdev_release_dma(filp);
  1771. priv->event_mask = 0;
  1772. spin_lock_irqsave(&chdev->pw_lock, flags);
  1773. if (!list_empty(&priv->pw_filters)) {
  1774. list_for_each_entry_safe(pw_filter, pw_filter_next,
  1775. &priv->pw_filters, priv_node)
  1776. rio_mport_delete_pw_filter(pw_filter);
  1777. }
  1778. spin_unlock_irqrestore(&chdev->pw_lock, flags);
  1779. spin_lock_irqsave(&chdev->db_lock, flags);
  1780. list_for_each_entry_safe(db_filter, db_filter_next,
  1781. &priv->db_filters, priv_node) {
  1782. rio_mport_delete_db_filter(db_filter);
  1783. }
  1784. spin_unlock_irqrestore(&chdev->db_lock, flags);
  1785. kfifo_free(&priv->event_fifo);
  1786. mutex_lock(&chdev->buf_mutex);
  1787. list_for_each_entry_safe(map, _map, &chdev->mappings, node) {
  1788. if (map->filp == filp) {
  1789. rmcd_debug(EXIT, "release mapping %p filp=%p",
  1790. map->virt_addr, filp);
  1791. kref_put(&map->ref, mport_release_mapping);
  1792. }
  1793. }
  1794. mutex_unlock(&chdev->buf_mutex);
  1795. mport_cdev_fasync(-1, filp, 0);
  1796. filp->private_data = NULL;
  1797. mutex_lock(&chdev->file_mutex);
  1798. list_del(&priv->list);
  1799. mutex_unlock(&chdev->file_mutex);
  1800. put_device(&chdev->dev);
  1801. kfree(priv);
  1802. return 0;
  1803. }
  1804. /*
  1805. * mport_cdev_ioctl() - IOCTLs for character device
  1806. */
  1807. static long mport_cdev_ioctl(struct file *filp,
  1808. unsigned int cmd, unsigned long arg)
  1809. {
  1810. int err = -EINVAL;
  1811. struct mport_cdev_priv *data = filp->private_data;
  1812. struct mport_dev *md = data->md;
  1813. if (atomic_read(&md->active) == 0)
  1814. return -ENODEV;
  1815. switch (cmd) {
  1816. case RIO_MPORT_MAINT_READ_LOCAL:
  1817. return rio_mport_maint_rd(data, (void __user *)arg, 1);
  1818. case RIO_MPORT_MAINT_WRITE_LOCAL:
  1819. return rio_mport_maint_wr(data, (void __user *)arg, 1);
  1820. case RIO_MPORT_MAINT_READ_REMOTE:
  1821. return rio_mport_maint_rd(data, (void __user *)arg, 0);
  1822. case RIO_MPORT_MAINT_WRITE_REMOTE:
  1823. return rio_mport_maint_wr(data, (void __user *)arg, 0);
  1824. case RIO_MPORT_MAINT_HDID_SET:
  1825. return maint_hdid_set(data, (void __user *)arg);
  1826. case RIO_MPORT_MAINT_COMPTAG_SET:
  1827. return maint_comptag_set(data, (void __user *)arg);
  1828. case RIO_MPORT_MAINT_PORT_IDX_GET:
  1829. return maint_port_idx_get(data, (void __user *)arg);
  1830. case RIO_MPORT_GET_PROPERTIES:
  1831. md->properties.hdid = md->mport->host_deviceid;
  1832. if (copy_to_user((void __user *)arg, &(md->properties),
  1833. sizeof(md->properties)))
  1834. return -EFAULT;
  1835. return 0;
  1836. case RIO_ENABLE_DOORBELL_RANGE:
  1837. return rio_mport_add_db_filter(data, (void __user *)arg);
  1838. case RIO_DISABLE_DOORBELL_RANGE:
  1839. return rio_mport_remove_db_filter(data, (void __user *)arg);
  1840. case RIO_ENABLE_PORTWRITE_RANGE:
  1841. return rio_mport_add_pw_filter(data, (void __user *)arg);
  1842. case RIO_DISABLE_PORTWRITE_RANGE:
  1843. return rio_mport_remove_pw_filter(data, (void __user *)arg);
  1844. case RIO_SET_EVENT_MASK:
  1845. data->event_mask = (u32)arg;
  1846. return 0;
  1847. case RIO_GET_EVENT_MASK:
  1848. if (copy_to_user((void __user *)arg, &data->event_mask,
  1849. sizeof(u32)))
  1850. return -EFAULT;
  1851. return 0;
  1852. case RIO_MAP_OUTBOUND:
  1853. return rio_mport_obw_map(filp, (void __user *)arg);
  1854. case RIO_MAP_INBOUND:
  1855. return rio_mport_map_inbound(filp, (void __user *)arg);
  1856. case RIO_UNMAP_OUTBOUND:
  1857. return rio_mport_obw_free(filp, (void __user *)arg);
  1858. case RIO_UNMAP_INBOUND:
  1859. return rio_mport_inbound_free(filp, (void __user *)arg);
  1860. case RIO_ALLOC_DMA:
  1861. return rio_mport_alloc_dma(filp, (void __user *)arg);
  1862. case RIO_FREE_DMA:
  1863. return rio_mport_free_dma(filp, (void __user *)arg);
  1864. case RIO_WAIT_FOR_ASYNC:
  1865. return rio_mport_wait_for_async_dma(filp, (void __user *)arg);
  1866. case RIO_TRANSFER:
  1867. return rio_mport_transfer_ioctl(filp, (void __user *)arg);
  1868. case RIO_DEV_ADD:
  1869. return rio_mport_add_riodev(data, (void __user *)arg);
  1870. case RIO_DEV_DEL:
  1871. return rio_mport_del_riodev(data, (void __user *)arg);
  1872. default:
  1873. break;
  1874. }
  1875. return err;
  1876. }
  1877. /*
  1878. * mport_release_mapping - free mapping resources and info structure
  1879. * @ref: a pointer to the kref within struct rio_mport_mapping
  1880. *
  1881. * NOTE: Shall be called while holding buf_mutex.
  1882. */
  1883. static void mport_release_mapping(struct kref *ref)
  1884. {
  1885. struct rio_mport_mapping *map =
  1886. container_of(ref, struct rio_mport_mapping, ref);
  1887. struct rio_mport *mport = map->md->mport;
  1888. rmcd_debug(MMAP, "type %d mapping @ %p (phys = %pad) for %s",
  1889. map->dir, map->virt_addr,
  1890. &map->phys_addr, mport->name);
  1891. list_del(&map->node);
  1892. switch (map->dir) {
  1893. case MAP_INBOUND:
  1894. rio_unmap_inb_region(mport, map->phys_addr);
  1895. case MAP_DMA:
  1896. dma_free_coherent(mport->dev.parent, map->size,
  1897. map->virt_addr, map->phys_addr);
  1898. break;
  1899. case MAP_OUTBOUND:
  1900. rio_unmap_outb_region(mport, map->rioid, map->rio_addr);
  1901. break;
  1902. }
  1903. kfree(map);
  1904. }
  1905. static void mport_mm_open(struct vm_area_struct *vma)
  1906. {
  1907. struct rio_mport_mapping *map = vma->vm_private_data;
  1908. rmcd_debug(MMAP, "%pad", &map->phys_addr);
  1909. kref_get(&map->ref);
  1910. }
  1911. static void mport_mm_close(struct vm_area_struct *vma)
  1912. {
  1913. struct rio_mport_mapping *map = vma->vm_private_data;
  1914. rmcd_debug(MMAP, "%pad", &map->phys_addr);
  1915. mutex_lock(&map->md->buf_mutex);
  1916. kref_put(&map->ref, mport_release_mapping);
  1917. mutex_unlock(&map->md->buf_mutex);
  1918. }
  1919. static const struct vm_operations_struct vm_ops = {
  1920. .open = mport_mm_open,
  1921. .close = mport_mm_close,
  1922. };
  1923. static int mport_cdev_mmap(struct file *filp, struct vm_area_struct *vma)
  1924. {
  1925. struct mport_cdev_priv *priv = filp->private_data;
  1926. struct mport_dev *md;
  1927. size_t size = vma->vm_end - vma->vm_start;
  1928. dma_addr_t baddr;
  1929. unsigned long offset;
  1930. int found = 0, ret;
  1931. struct rio_mport_mapping *map;
  1932. rmcd_debug(MMAP, "0x%x bytes at offset 0x%lx",
  1933. (unsigned int)size, vma->vm_pgoff);
  1934. md = priv->md;
  1935. baddr = ((dma_addr_t)vma->vm_pgoff << PAGE_SHIFT);
  1936. mutex_lock(&md->buf_mutex);
  1937. list_for_each_entry(map, &md->mappings, node) {
  1938. if (baddr >= map->phys_addr &&
  1939. baddr < (map->phys_addr + map->size)) {
  1940. found = 1;
  1941. break;
  1942. }
  1943. }
  1944. mutex_unlock(&md->buf_mutex);
  1945. if (!found)
  1946. return -ENOMEM;
  1947. offset = baddr - map->phys_addr;
  1948. if (size + offset > map->size)
  1949. return -EINVAL;
  1950. vma->vm_pgoff = offset >> PAGE_SHIFT;
  1951. rmcd_debug(MMAP, "MMAP adjusted offset = 0x%lx", vma->vm_pgoff);
  1952. if (map->dir == MAP_INBOUND || map->dir == MAP_DMA)
  1953. ret = dma_mmap_coherent(md->mport->dev.parent, vma,
  1954. map->virt_addr, map->phys_addr, map->size);
  1955. else if (map->dir == MAP_OUTBOUND) {
  1956. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1957. ret = vm_iomap_memory(vma, map->phys_addr, map->size);
  1958. } else {
  1959. rmcd_error("Attempt to mmap unsupported mapping type");
  1960. ret = -EIO;
  1961. }
  1962. if (!ret) {
  1963. vma->vm_private_data = map;
  1964. vma->vm_ops = &vm_ops;
  1965. mport_mm_open(vma);
  1966. } else {
  1967. rmcd_error("MMAP exit with err=%d", ret);
  1968. }
  1969. return ret;
  1970. }
  1971. static unsigned int mport_cdev_poll(struct file *filp, poll_table *wait)
  1972. {
  1973. struct mport_cdev_priv *priv = filp->private_data;
  1974. poll_wait(filp, &priv->event_rx_wait, wait);
  1975. if (kfifo_len(&priv->event_fifo))
  1976. return POLLIN | POLLRDNORM;
  1977. return 0;
  1978. }
  1979. static ssize_t mport_read(struct file *filp, char __user *buf, size_t count,
  1980. loff_t *ppos)
  1981. {
  1982. struct mport_cdev_priv *priv = filp->private_data;
  1983. int copied;
  1984. ssize_t ret;
  1985. if (!count)
  1986. return 0;
  1987. if (kfifo_is_empty(&priv->event_fifo) &&
  1988. (filp->f_flags & O_NONBLOCK))
  1989. return -EAGAIN;
  1990. if (count % sizeof(struct rio_event))
  1991. return -EINVAL;
  1992. ret = wait_event_interruptible(priv->event_rx_wait,
  1993. kfifo_len(&priv->event_fifo) != 0);
  1994. if (ret)
  1995. return ret;
  1996. while (ret < count) {
  1997. if (kfifo_to_user(&priv->event_fifo, buf,
  1998. sizeof(struct rio_event), &copied))
  1999. return -EFAULT;
  2000. ret += copied;
  2001. buf += copied;
  2002. }
  2003. return ret;
  2004. }
  2005. static ssize_t mport_write(struct file *filp, const char __user *buf,
  2006. size_t count, loff_t *ppos)
  2007. {
  2008. struct mport_cdev_priv *priv = filp->private_data;
  2009. struct rio_mport *mport = priv->md->mport;
  2010. struct rio_event event;
  2011. int len, ret;
  2012. if (!count)
  2013. return 0;
  2014. if (count % sizeof(event))
  2015. return -EINVAL;
  2016. len = 0;
  2017. while ((count - len) >= (int)sizeof(event)) {
  2018. if (copy_from_user(&event, buf, sizeof(event)))
  2019. return -EFAULT;
  2020. if (event.header != RIO_DOORBELL)
  2021. return -EINVAL;
  2022. ret = rio_mport_send_doorbell(mport,
  2023. event.u.doorbell.rioid,
  2024. event.u.doorbell.payload);
  2025. if (ret < 0)
  2026. return ret;
  2027. len += sizeof(event);
  2028. buf += sizeof(event);
  2029. }
  2030. return len;
  2031. }
  2032. static const struct file_operations mport_fops = {
  2033. .owner = THIS_MODULE,
  2034. .open = mport_cdev_open,
  2035. .release = mport_cdev_release,
  2036. .poll = mport_cdev_poll,
  2037. .read = mport_read,
  2038. .write = mport_write,
  2039. .mmap = mport_cdev_mmap,
  2040. .fasync = mport_cdev_fasync,
  2041. .unlocked_ioctl = mport_cdev_ioctl
  2042. };
  2043. /*
  2044. * Character device management
  2045. */
  2046. static void mport_device_release(struct device *dev)
  2047. {
  2048. struct mport_dev *md;
  2049. rmcd_debug(EXIT, "%s", dev_name(dev));
  2050. md = container_of(dev, struct mport_dev, dev);
  2051. kfree(md);
  2052. }
  2053. /*
  2054. * mport_cdev_add() - Create mport_dev from rio_mport
  2055. * @mport: RapidIO master port
  2056. */
  2057. static struct mport_dev *mport_cdev_add(struct rio_mport *mport)
  2058. {
  2059. int ret = 0;
  2060. struct mport_dev *md;
  2061. struct rio_mport_attr attr;
  2062. md = kzalloc(sizeof(*md), GFP_KERNEL);
  2063. if (!md) {
  2064. rmcd_error("Unable allocate a device object");
  2065. return NULL;
  2066. }
  2067. md->mport = mport;
  2068. mutex_init(&md->buf_mutex);
  2069. mutex_init(&md->file_mutex);
  2070. INIT_LIST_HEAD(&md->file_list);
  2071. cdev_init(&md->cdev, &mport_fops);
  2072. md->cdev.owner = THIS_MODULE;
  2073. ret = cdev_add(&md->cdev, MKDEV(MAJOR(dev_number), mport->id), 1);
  2074. if (ret < 0) {
  2075. kfree(md);
  2076. rmcd_error("Unable to register a device, err=%d", ret);
  2077. return NULL;
  2078. }
  2079. md->dev.devt = md->cdev.dev;
  2080. md->dev.class = dev_class;
  2081. md->dev.parent = &mport->dev;
  2082. md->dev.release = mport_device_release;
  2083. dev_set_name(&md->dev, DEV_NAME "%d", mport->id);
  2084. atomic_set(&md->active, 1);
  2085. ret = device_register(&md->dev);
  2086. if (ret) {
  2087. rmcd_error("Failed to register mport %d (err=%d)",
  2088. mport->id, ret);
  2089. goto err_cdev;
  2090. }
  2091. get_device(&md->dev);
  2092. INIT_LIST_HEAD(&md->doorbells);
  2093. spin_lock_init(&md->db_lock);
  2094. INIT_LIST_HEAD(&md->portwrites);
  2095. spin_lock_init(&md->pw_lock);
  2096. INIT_LIST_HEAD(&md->mappings);
  2097. md->properties.id = mport->id;
  2098. md->properties.sys_size = mport->sys_size;
  2099. md->properties.hdid = mport->host_deviceid;
  2100. md->properties.index = mport->index;
  2101. /* The transfer_mode property will be returned through mport query
  2102. * interface
  2103. */
  2104. #ifdef CONFIG_FSL_RIO /* for now: only on Freescale's SoCs */
  2105. md->properties.transfer_mode |= RIO_TRANSFER_MODE_MAPPED;
  2106. #else
  2107. md->properties.transfer_mode |= RIO_TRANSFER_MODE_TRANSFER;
  2108. #endif
  2109. ret = rio_query_mport(mport, &attr);
  2110. if (!ret) {
  2111. md->properties.flags = attr.flags;
  2112. md->properties.link_speed = attr.link_speed;
  2113. md->properties.link_width = attr.link_width;
  2114. md->properties.dma_max_sge = attr.dma_max_sge;
  2115. md->properties.dma_max_size = attr.dma_max_size;
  2116. md->properties.dma_align = attr.dma_align;
  2117. md->properties.cap_sys_size = 0;
  2118. md->properties.cap_transfer_mode = 0;
  2119. md->properties.cap_addr_size = 0;
  2120. } else
  2121. pr_info(DRV_PREFIX "Failed to obtain info for %s cdev(%d:%d)\n",
  2122. mport->name, MAJOR(dev_number), mport->id);
  2123. mutex_lock(&mport_devs_lock);
  2124. list_add_tail(&md->node, &mport_devs);
  2125. mutex_unlock(&mport_devs_lock);
  2126. pr_info(DRV_PREFIX "Added %s cdev(%d:%d)\n",
  2127. mport->name, MAJOR(dev_number), mport->id);
  2128. return md;
  2129. err_cdev:
  2130. cdev_del(&md->cdev);
  2131. kfree(md);
  2132. return NULL;
  2133. }
  2134. /*
  2135. * mport_cdev_terminate_dma() - Stop all active DMA data transfers and release
  2136. * associated DMA channels.
  2137. */
  2138. static void mport_cdev_terminate_dma(struct mport_dev *md)
  2139. {
  2140. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  2141. struct mport_cdev_priv *client;
  2142. rmcd_debug(DMA, "%s", dev_name(&md->dev));
  2143. mutex_lock(&md->file_mutex);
  2144. list_for_each_entry(client, &md->file_list, list) {
  2145. if (client->dmach) {
  2146. dmaengine_terminate_all(client->dmach);
  2147. rio_release_dma(client->dmach);
  2148. }
  2149. }
  2150. mutex_unlock(&md->file_mutex);
  2151. if (md->dma_chan) {
  2152. dmaengine_terminate_all(md->dma_chan);
  2153. rio_release_dma(md->dma_chan);
  2154. md->dma_chan = NULL;
  2155. }
  2156. #endif
  2157. }
  2158. /*
  2159. * mport_cdev_kill_fasync() - Send SIGIO signal to all processes with open
  2160. * mport_cdev files.
  2161. */
  2162. static int mport_cdev_kill_fasync(struct mport_dev *md)
  2163. {
  2164. unsigned int files = 0;
  2165. struct mport_cdev_priv *client;
  2166. mutex_lock(&md->file_mutex);
  2167. list_for_each_entry(client, &md->file_list, list) {
  2168. if (client->async_queue)
  2169. kill_fasync(&client->async_queue, SIGIO, POLL_HUP);
  2170. files++;
  2171. }
  2172. mutex_unlock(&md->file_mutex);
  2173. return files;
  2174. }
  2175. /*
  2176. * mport_cdev_remove() - Remove mport character device
  2177. * @dev: Mport device to remove
  2178. */
  2179. static void mport_cdev_remove(struct mport_dev *md)
  2180. {
  2181. struct rio_mport_mapping *map, *_map;
  2182. rmcd_debug(EXIT, "Remove %s cdev", md->mport->name);
  2183. atomic_set(&md->active, 0);
  2184. mport_cdev_terminate_dma(md);
  2185. rio_del_mport_pw_handler(md->mport, md, rio_mport_pw_handler);
  2186. cdev_del(&(md->cdev));
  2187. mport_cdev_kill_fasync(md);
  2188. flush_workqueue(dma_wq);
  2189. /* TODO: do we need to give clients some time to close file
  2190. * descriptors? Simple wait for XX, or kref?
  2191. */
  2192. /*
  2193. * Release DMA buffers allocated for the mport device.
  2194. * Disable associated inbound Rapidio requests mapping if applicable.
  2195. */
  2196. mutex_lock(&md->buf_mutex);
  2197. list_for_each_entry_safe(map, _map, &md->mappings, node) {
  2198. kref_put(&map->ref, mport_release_mapping);
  2199. }
  2200. mutex_unlock(&md->buf_mutex);
  2201. if (!list_empty(&md->mappings))
  2202. rmcd_warn("WARNING: %s pending mappings on removal",
  2203. md->mport->name);
  2204. rio_release_inb_dbell(md->mport, 0, 0x0fff);
  2205. device_unregister(&md->dev);
  2206. put_device(&md->dev);
  2207. }
  2208. /*
  2209. * RIO rio_mport_interface driver
  2210. */
  2211. /*
  2212. * mport_add_mport() - Add rio_mport from LDM device struct
  2213. * @dev: Linux device model struct
  2214. * @class_intf: Linux class_interface
  2215. */
  2216. static int mport_add_mport(struct device *dev,
  2217. struct class_interface *class_intf)
  2218. {
  2219. struct rio_mport *mport = NULL;
  2220. struct mport_dev *chdev = NULL;
  2221. mport = to_rio_mport(dev);
  2222. if (!mport)
  2223. return -ENODEV;
  2224. chdev = mport_cdev_add(mport);
  2225. if (!chdev)
  2226. return -ENODEV;
  2227. return 0;
  2228. }
  2229. /*
  2230. * mport_remove_mport() - Remove rio_mport from global list
  2231. * TODO remove device from global mport_dev list
  2232. */
  2233. static void mport_remove_mport(struct device *dev,
  2234. struct class_interface *class_intf)
  2235. {
  2236. struct rio_mport *mport = NULL;
  2237. struct mport_dev *chdev;
  2238. int found = 0;
  2239. mport = to_rio_mport(dev);
  2240. rmcd_debug(EXIT, "Remove %s", mport->name);
  2241. mutex_lock(&mport_devs_lock);
  2242. list_for_each_entry(chdev, &mport_devs, node) {
  2243. if (chdev->mport->id == mport->id) {
  2244. atomic_set(&chdev->active, 0);
  2245. list_del(&chdev->node);
  2246. found = 1;
  2247. break;
  2248. }
  2249. }
  2250. mutex_unlock(&mport_devs_lock);
  2251. if (found)
  2252. mport_cdev_remove(chdev);
  2253. }
  2254. /* the rio_mport_interface is used to handle local mport devices */
  2255. static struct class_interface rio_mport_interface __refdata = {
  2256. .class = &rio_mport_class,
  2257. .add_dev = mport_add_mport,
  2258. .remove_dev = mport_remove_mport,
  2259. };
  2260. /*
  2261. * Linux kernel module
  2262. */
  2263. /*
  2264. * mport_init - Driver module loading
  2265. */
  2266. static int __init mport_init(void)
  2267. {
  2268. int ret;
  2269. /* Create device class needed by udev */
  2270. dev_class = class_create(THIS_MODULE, DRV_NAME);
  2271. if (IS_ERR(dev_class)) {
  2272. rmcd_error("Unable to create " DRV_NAME " class");
  2273. return PTR_ERR(dev_class);
  2274. }
  2275. ret = alloc_chrdev_region(&dev_number, 0, RIO_MAX_MPORTS, DRV_NAME);
  2276. if (ret < 0)
  2277. goto err_chr;
  2278. rmcd_debug(INIT, "Registered class with major=%d", MAJOR(dev_number));
  2279. /* Register to rio_mport_interface */
  2280. ret = class_interface_register(&rio_mport_interface);
  2281. if (ret) {
  2282. rmcd_error("class_interface_register() failed, err=%d", ret);
  2283. goto err_cli;
  2284. }
  2285. dma_wq = create_singlethread_workqueue("dma_wq");
  2286. if (!dma_wq) {
  2287. rmcd_error("failed to create DMA work queue");
  2288. ret = -ENOMEM;
  2289. goto err_wq;
  2290. }
  2291. return 0;
  2292. err_wq:
  2293. class_interface_unregister(&rio_mport_interface);
  2294. err_cli:
  2295. unregister_chrdev_region(dev_number, RIO_MAX_MPORTS);
  2296. err_chr:
  2297. class_destroy(dev_class);
  2298. return ret;
  2299. }
  2300. /**
  2301. * mport_exit - Driver module unloading
  2302. */
  2303. static void __exit mport_exit(void)
  2304. {
  2305. class_interface_unregister(&rio_mport_interface);
  2306. class_destroy(dev_class);
  2307. unregister_chrdev_region(dev_number, RIO_MAX_MPORTS);
  2308. destroy_workqueue(dma_wq);
  2309. }
  2310. module_init(mport_init);
  2311. module_exit(mport_exit);