rio_mport_cdev.c 67 KB

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