rio_mport_cdev.c 67 KB

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