rio_cm.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  1. /*
  2. * rio_cm - RapidIO Channelized Messaging Driver
  3. *
  4. * Copyright 2013-2016 Integrated Device Technology, Inc.
  5. * Copyright (c) 2015, Prodrive Technologies
  6. * Copyright (c) 2015, RapidIO Trade Association
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
  14. * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE
  16. * GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include <linux/rio.h>
  24. #include <linux/rio_drv.h>
  25. #include <linux/slab.h>
  26. #include <linux/idr.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/cdev.h>
  29. #include <linux/fs.h>
  30. #include <linux/poll.h>
  31. #include <linux/reboot.h>
  32. #include <linux/bitops.h>
  33. #include <linux/printk.h>
  34. #include <linux/rio_cm_cdev.h>
  35. #define DRV_NAME "rio_cm"
  36. #define DRV_VERSION "1.0.0"
  37. #define DRV_AUTHOR "Alexandre Bounine <alexandre.bounine@idt.com>"
  38. #define DRV_DESC "RapidIO Channelized Messaging Driver"
  39. #define DEV_NAME "rio_cm"
  40. /* Debug output filtering masks */
  41. enum {
  42. DBG_NONE = 0,
  43. DBG_INIT = BIT(0), /* driver init */
  44. DBG_EXIT = BIT(1), /* driver exit */
  45. DBG_MPORT = BIT(2), /* mport add/remove */
  46. DBG_RDEV = BIT(3), /* RapidIO device add/remove */
  47. DBG_CHOP = BIT(4), /* channel operations */
  48. DBG_WAIT = BIT(5), /* waiting for events */
  49. DBG_TX = BIT(6), /* message TX */
  50. DBG_TX_EVENT = BIT(7), /* message TX event */
  51. DBG_RX_DATA = BIT(8), /* inbound data messages */
  52. DBG_RX_CMD = BIT(9), /* inbound REQ/ACK/NACK messages */
  53. DBG_ALL = ~0,
  54. };
  55. #ifdef DEBUG
  56. #define riocm_debug(level, fmt, arg...) \
  57. do { \
  58. if (DBG_##level & dbg_level) \
  59. pr_debug(DRV_NAME ": %s " fmt "\n", \
  60. __func__, ##arg); \
  61. } while (0)
  62. #else
  63. #define riocm_debug(level, fmt, arg...) \
  64. no_printk(KERN_DEBUG pr_fmt(DRV_NAME fmt "\n"), ##arg)
  65. #endif
  66. #define riocm_warn(fmt, arg...) \
  67. pr_warn(DRV_NAME ": %s WARNING " fmt "\n", __func__, ##arg)
  68. #define riocm_error(fmt, arg...) \
  69. pr_err(DRV_NAME ": %s ERROR " fmt "\n", __func__, ##arg)
  70. static int cmbox = 1;
  71. module_param(cmbox, int, S_IRUGO);
  72. MODULE_PARM_DESC(cmbox, "RapidIO Mailbox number (default 1)");
  73. static int chstart = 256;
  74. module_param(chstart, int, S_IRUGO);
  75. MODULE_PARM_DESC(chstart,
  76. "Start channel number for dynamic allocation (default 256)");
  77. #ifdef DEBUG
  78. static u32 dbg_level = DBG_NONE;
  79. module_param(dbg_level, uint, S_IWUSR | S_IRUGO);
  80. MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
  81. #endif
  82. MODULE_AUTHOR(DRV_AUTHOR);
  83. MODULE_DESCRIPTION(DRV_DESC);
  84. MODULE_LICENSE("GPL");
  85. MODULE_VERSION(DRV_VERSION);
  86. #define RIOCM_TX_RING_SIZE 128
  87. #define RIOCM_RX_RING_SIZE 128
  88. #define RIOCM_CONNECT_TO 3 /* connect response TO (in sec) */
  89. #define RIOCM_MAX_CHNUM 0xffff /* Use full range of u16 field */
  90. #define RIOCM_CHNUM_AUTO 0
  91. #define RIOCM_MAX_EP_COUNT 0x10000 /* Max number of endpoints */
  92. enum rio_cm_state {
  93. RIO_CM_IDLE,
  94. RIO_CM_CONNECT,
  95. RIO_CM_CONNECTED,
  96. RIO_CM_DISCONNECT,
  97. RIO_CM_CHAN_BOUND,
  98. RIO_CM_LISTEN,
  99. RIO_CM_DESTROYING,
  100. };
  101. enum rio_cm_pkt_type {
  102. RIO_CM_SYS = 0xaa,
  103. RIO_CM_CHAN = 0x55,
  104. };
  105. enum rio_cm_chop {
  106. CM_CONN_REQ,
  107. CM_CONN_ACK,
  108. CM_CONN_CLOSE,
  109. CM_DATA_MSG,
  110. };
  111. struct rio_ch_base_bhdr {
  112. u32 src_id;
  113. u32 dst_id;
  114. #define RIO_HDR_LETTER_MASK 0xffff0000
  115. #define RIO_HDR_MBOX_MASK 0x0000ffff
  116. u8 src_mbox;
  117. u8 dst_mbox;
  118. u8 type;
  119. } __attribute__((__packed__));
  120. struct rio_ch_chan_hdr {
  121. struct rio_ch_base_bhdr bhdr;
  122. u8 ch_op;
  123. u16 dst_ch;
  124. u16 src_ch;
  125. u16 msg_len;
  126. u16 rsrvd;
  127. } __attribute__((__packed__));
  128. struct tx_req {
  129. struct list_head node;
  130. struct rio_dev *rdev;
  131. void *buffer;
  132. size_t len;
  133. };
  134. struct cm_dev {
  135. struct list_head list;
  136. struct rio_mport *mport;
  137. void *rx_buf[RIOCM_RX_RING_SIZE];
  138. int rx_slots;
  139. struct mutex rx_lock;
  140. void *tx_buf[RIOCM_TX_RING_SIZE];
  141. int tx_slot;
  142. int tx_cnt;
  143. int tx_ack_slot;
  144. struct list_head tx_reqs;
  145. spinlock_t tx_lock;
  146. struct list_head peers;
  147. u32 npeers;
  148. struct workqueue_struct *rx_wq;
  149. struct work_struct rx_work;
  150. };
  151. struct chan_rx_ring {
  152. void *buf[RIOCM_RX_RING_SIZE];
  153. int head;
  154. int tail;
  155. int count;
  156. /* Tracking RX buffers reported to upper level */
  157. void *inuse[RIOCM_RX_RING_SIZE];
  158. int inuse_cnt;
  159. };
  160. struct rio_channel {
  161. u16 id; /* local channel ID */
  162. struct kref ref; /* channel refcount */
  163. struct file *filp;
  164. struct cm_dev *cmdev; /* associated CM device object */
  165. struct rio_dev *rdev; /* remote RapidIO device */
  166. enum rio_cm_state state;
  167. int error;
  168. spinlock_t lock;
  169. void *context;
  170. u32 loc_destid; /* local destID */
  171. u32 rem_destid; /* remote destID */
  172. u16 rem_channel; /* remote channel ID */
  173. struct list_head accept_queue;
  174. struct list_head ch_node;
  175. struct completion comp;
  176. struct completion comp_close;
  177. struct chan_rx_ring rx_ring;
  178. };
  179. struct cm_peer {
  180. struct list_head node;
  181. struct rio_dev *rdev;
  182. };
  183. struct rio_cm_work {
  184. struct work_struct work;
  185. struct cm_dev *cm;
  186. void *data;
  187. };
  188. struct conn_req {
  189. struct list_head node;
  190. u32 destid; /* requester destID */
  191. u16 chan; /* requester channel ID */
  192. struct cm_dev *cmdev;
  193. };
  194. /*
  195. * A channel_dev structure represents a CM_CDEV
  196. * @cdev Character device
  197. * @dev Associated device object
  198. */
  199. struct channel_dev {
  200. struct cdev cdev;
  201. struct device *dev;
  202. };
  203. static struct rio_channel *riocm_ch_alloc(u16 ch_num);
  204. static void riocm_ch_free(struct kref *ref);
  205. static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
  206. void *buffer, size_t len);
  207. static int riocm_ch_close(struct rio_channel *ch);
  208. static DEFINE_SPINLOCK(idr_lock);
  209. static DEFINE_IDR(ch_idr);
  210. static LIST_HEAD(cm_dev_list);
  211. static DECLARE_RWSEM(rdev_sem);
  212. static struct class *dev_class;
  213. static unsigned int dev_major;
  214. static unsigned int dev_minor_base;
  215. static dev_t dev_number;
  216. static struct channel_dev riocm_cdev;
  217. #define is_msg_capable(src_ops, dst_ops) \
  218. ((src_ops & RIO_SRC_OPS_DATA_MSG) && \
  219. (dst_ops & RIO_DST_OPS_DATA_MSG))
  220. #define dev_cm_capable(dev) \
  221. is_msg_capable(dev->src_ops, dev->dst_ops)
  222. static int riocm_cmp(struct rio_channel *ch, enum rio_cm_state cmp)
  223. {
  224. int ret;
  225. spin_lock_bh(&ch->lock);
  226. ret = (ch->state == cmp);
  227. spin_unlock_bh(&ch->lock);
  228. return ret;
  229. }
  230. static int riocm_cmp_exch(struct rio_channel *ch,
  231. enum rio_cm_state cmp, enum rio_cm_state exch)
  232. {
  233. int ret;
  234. spin_lock_bh(&ch->lock);
  235. ret = (ch->state == cmp);
  236. if (ret)
  237. ch->state = exch;
  238. spin_unlock_bh(&ch->lock);
  239. return ret;
  240. }
  241. static enum rio_cm_state riocm_exch(struct rio_channel *ch,
  242. enum rio_cm_state exch)
  243. {
  244. enum rio_cm_state old;
  245. spin_lock_bh(&ch->lock);
  246. old = ch->state;
  247. ch->state = exch;
  248. spin_unlock_bh(&ch->lock);
  249. return old;
  250. }
  251. static struct rio_channel *riocm_get_channel(u16 nr)
  252. {
  253. struct rio_channel *ch;
  254. spin_lock_bh(&idr_lock);
  255. ch = idr_find(&ch_idr, nr);
  256. if (ch)
  257. kref_get(&ch->ref);
  258. spin_unlock_bh(&idr_lock);
  259. return ch;
  260. }
  261. static void riocm_put_channel(struct rio_channel *ch)
  262. {
  263. kref_put(&ch->ref, riocm_ch_free);
  264. }
  265. static void *riocm_rx_get_msg(struct cm_dev *cm)
  266. {
  267. void *msg;
  268. int i;
  269. msg = rio_get_inb_message(cm->mport, cmbox);
  270. if (msg) {
  271. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  272. if (cm->rx_buf[i] == msg) {
  273. cm->rx_buf[i] = NULL;
  274. cm->rx_slots++;
  275. break;
  276. }
  277. }
  278. if (i == RIOCM_RX_RING_SIZE)
  279. riocm_warn("no record for buffer 0x%p", msg);
  280. }
  281. return msg;
  282. }
  283. /*
  284. * riocm_rx_fill - fills a ring of receive buffers for given cm device
  285. * @cm: cm_dev object
  286. * @nent: max number of entries to fill
  287. *
  288. * Returns: none
  289. */
  290. static void riocm_rx_fill(struct cm_dev *cm, int nent)
  291. {
  292. int i;
  293. if (cm->rx_slots == 0)
  294. return;
  295. for (i = 0; i < RIOCM_RX_RING_SIZE && cm->rx_slots && nent; i++) {
  296. if (cm->rx_buf[i] == NULL) {
  297. cm->rx_buf[i] = kmalloc(RIO_MAX_MSG_SIZE, GFP_KERNEL);
  298. if (cm->rx_buf[i] == NULL)
  299. break;
  300. rio_add_inb_buffer(cm->mport, cmbox, cm->rx_buf[i]);
  301. cm->rx_slots--;
  302. nent--;
  303. }
  304. }
  305. }
  306. /*
  307. * riocm_rx_free - frees all receive buffers associated with given cm device
  308. * @cm: cm_dev object
  309. *
  310. * Returns: none
  311. */
  312. static void riocm_rx_free(struct cm_dev *cm)
  313. {
  314. int i;
  315. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  316. if (cm->rx_buf[i] != NULL) {
  317. kfree(cm->rx_buf[i]);
  318. cm->rx_buf[i] = NULL;
  319. }
  320. }
  321. }
  322. /*
  323. * riocm_req_handler - connection request handler
  324. * @cm: cm_dev object
  325. * @req_data: pointer to the request packet
  326. *
  327. * Returns: 0 if success, or
  328. * -EINVAL if channel is not in correct state,
  329. * -ENODEV if cannot find a channel with specified ID,
  330. * -ENOMEM if unable to allocate memory to store the request
  331. */
  332. static int riocm_req_handler(struct cm_dev *cm, void *req_data)
  333. {
  334. struct rio_channel *ch;
  335. struct conn_req *req;
  336. struct rio_ch_chan_hdr *hh = req_data;
  337. u16 chnum;
  338. chnum = ntohs(hh->dst_ch);
  339. ch = riocm_get_channel(chnum);
  340. if (!ch)
  341. return -ENODEV;
  342. if (ch->state != RIO_CM_LISTEN) {
  343. riocm_debug(RX_CMD, "channel %d is not in listen state", chnum);
  344. riocm_put_channel(ch);
  345. return -EINVAL;
  346. }
  347. req = kzalloc(sizeof(*req), GFP_KERNEL);
  348. if (!req) {
  349. riocm_put_channel(ch);
  350. return -ENOMEM;
  351. }
  352. req->destid = ntohl(hh->bhdr.src_id);
  353. req->chan = ntohs(hh->src_ch);
  354. req->cmdev = cm;
  355. spin_lock_bh(&ch->lock);
  356. list_add_tail(&req->node, &ch->accept_queue);
  357. spin_unlock_bh(&ch->lock);
  358. complete(&ch->comp);
  359. riocm_put_channel(ch);
  360. return 0;
  361. }
  362. /*
  363. * riocm_resp_handler - response to connection request handler
  364. * @resp_data: pointer to the response packet
  365. *
  366. * Returns: 0 if success, or
  367. * -EINVAL if channel is not in correct state,
  368. * -ENODEV if cannot find a channel with specified ID,
  369. */
  370. static int riocm_resp_handler(void *resp_data)
  371. {
  372. struct rio_channel *ch;
  373. struct rio_ch_chan_hdr *hh = resp_data;
  374. u16 chnum;
  375. chnum = ntohs(hh->dst_ch);
  376. ch = riocm_get_channel(chnum);
  377. if (!ch)
  378. return -ENODEV;
  379. if (ch->state != RIO_CM_CONNECT) {
  380. riocm_put_channel(ch);
  381. return -EINVAL;
  382. }
  383. riocm_exch(ch, RIO_CM_CONNECTED);
  384. ch->rem_channel = ntohs(hh->src_ch);
  385. complete(&ch->comp);
  386. riocm_put_channel(ch);
  387. return 0;
  388. }
  389. /*
  390. * riocm_close_handler - channel close request handler
  391. * @req_data: pointer to the request packet
  392. *
  393. * Returns: 0 if success, or
  394. * -ENODEV if cannot find a channel with specified ID,
  395. * + error codes returned by riocm_ch_close.
  396. */
  397. static int riocm_close_handler(void *data)
  398. {
  399. struct rio_channel *ch;
  400. struct rio_ch_chan_hdr *hh = data;
  401. int ret;
  402. riocm_debug(RX_CMD, "for ch=%d", ntohs(hh->dst_ch));
  403. spin_lock_bh(&idr_lock);
  404. ch = idr_find(&ch_idr, ntohs(hh->dst_ch));
  405. if (!ch) {
  406. spin_unlock_bh(&idr_lock);
  407. return -ENODEV;
  408. }
  409. idr_remove(&ch_idr, ch->id);
  410. spin_unlock_bh(&idr_lock);
  411. riocm_exch(ch, RIO_CM_DISCONNECT);
  412. ret = riocm_ch_close(ch);
  413. if (ret)
  414. riocm_debug(RX_CMD, "riocm_ch_close() returned %d", ret);
  415. return 0;
  416. }
  417. /*
  418. * rio_cm_handler - function that services request (non-data) packets
  419. * @cm: cm_dev object
  420. * @data: pointer to the packet
  421. */
  422. static void rio_cm_handler(struct cm_dev *cm, void *data)
  423. {
  424. struct rio_ch_chan_hdr *hdr;
  425. if (!rio_mport_is_running(cm->mport))
  426. goto out;
  427. hdr = data;
  428. riocm_debug(RX_CMD, "OP=%x for ch=%d from %d",
  429. hdr->ch_op, ntohs(hdr->dst_ch), ntohs(hdr->src_ch));
  430. switch (hdr->ch_op) {
  431. case CM_CONN_REQ:
  432. riocm_req_handler(cm, data);
  433. break;
  434. case CM_CONN_ACK:
  435. riocm_resp_handler(data);
  436. break;
  437. case CM_CONN_CLOSE:
  438. riocm_close_handler(data);
  439. break;
  440. default:
  441. riocm_error("Invalid packet header");
  442. break;
  443. }
  444. out:
  445. kfree(data);
  446. }
  447. /*
  448. * rio_rx_data_handler - received data packet handler
  449. * @cm: cm_dev object
  450. * @buf: data packet
  451. *
  452. * Returns: 0 if success, or
  453. * -ENODEV if cannot find a channel with specified ID,
  454. * -EIO if channel is not in CONNECTED state,
  455. * -ENOMEM if channel RX queue is full (packet discarded)
  456. */
  457. static int rio_rx_data_handler(struct cm_dev *cm, void *buf)
  458. {
  459. struct rio_ch_chan_hdr *hdr;
  460. struct rio_channel *ch;
  461. hdr = buf;
  462. riocm_debug(RX_DATA, "for ch=%d", ntohs(hdr->dst_ch));
  463. ch = riocm_get_channel(ntohs(hdr->dst_ch));
  464. if (!ch) {
  465. /* Discard data message for non-existing channel */
  466. kfree(buf);
  467. return -ENODEV;
  468. }
  469. /* Place pointer to the buffer into channel's RX queue */
  470. spin_lock(&ch->lock);
  471. if (ch->state != RIO_CM_CONNECTED) {
  472. /* Channel is not ready to receive data, discard a packet */
  473. riocm_debug(RX_DATA, "ch=%d is in wrong state=%d",
  474. ch->id, ch->state);
  475. spin_unlock(&ch->lock);
  476. kfree(buf);
  477. riocm_put_channel(ch);
  478. return -EIO;
  479. }
  480. if (ch->rx_ring.count == RIOCM_RX_RING_SIZE) {
  481. /* If RX ring is full, discard a packet */
  482. riocm_debug(RX_DATA, "ch=%d is full", ch->id);
  483. spin_unlock(&ch->lock);
  484. kfree(buf);
  485. riocm_put_channel(ch);
  486. return -ENOMEM;
  487. }
  488. ch->rx_ring.buf[ch->rx_ring.head] = buf;
  489. ch->rx_ring.head++;
  490. ch->rx_ring.count++;
  491. ch->rx_ring.head %= RIOCM_RX_RING_SIZE;
  492. complete(&ch->comp);
  493. spin_unlock(&ch->lock);
  494. riocm_put_channel(ch);
  495. return 0;
  496. }
  497. /*
  498. * rio_ibmsg_handler - inbound message packet handler
  499. */
  500. static void rio_ibmsg_handler(struct work_struct *work)
  501. {
  502. struct cm_dev *cm = container_of(work, struct cm_dev, rx_work);
  503. void *data;
  504. struct rio_ch_chan_hdr *hdr;
  505. if (!rio_mport_is_running(cm->mport))
  506. return;
  507. while (1) {
  508. mutex_lock(&cm->rx_lock);
  509. data = riocm_rx_get_msg(cm);
  510. if (data)
  511. riocm_rx_fill(cm, 1);
  512. mutex_unlock(&cm->rx_lock);
  513. if (data == NULL)
  514. break;
  515. hdr = data;
  516. if (hdr->bhdr.type != RIO_CM_CHAN) {
  517. /* For now simply discard packets other than channel */
  518. riocm_error("Unsupported TYPE code (0x%x). Msg dropped",
  519. hdr->bhdr.type);
  520. kfree(data);
  521. continue;
  522. }
  523. /* Process a channel message */
  524. if (hdr->ch_op == CM_DATA_MSG)
  525. rio_rx_data_handler(cm, data);
  526. else
  527. rio_cm_handler(cm, data);
  528. }
  529. }
  530. static void riocm_inb_msg_event(struct rio_mport *mport, void *dev_id,
  531. int mbox, int slot)
  532. {
  533. struct cm_dev *cm = dev_id;
  534. if (rio_mport_is_running(cm->mport) && !work_pending(&cm->rx_work))
  535. queue_work(cm->rx_wq, &cm->rx_work);
  536. }
  537. /*
  538. * rio_txcq_handler - TX completion handler
  539. * @cm: cm_dev object
  540. * @slot: TX queue slot
  541. *
  542. * TX completion handler also ensures that pending request packets are placed
  543. * into transmit queue as soon as a free slot becomes available. This is done
  544. * to give higher priority to request packets during high intensity data flow.
  545. */
  546. static void rio_txcq_handler(struct cm_dev *cm, int slot)
  547. {
  548. int ack_slot;
  549. /* ATTN: Add TX completion notification if/when direct buffer
  550. * transfer is implemented. At this moment only correct tracking
  551. * of tx_count is important.
  552. */
  553. riocm_debug(TX_EVENT, "for mport_%d slot %d tx_cnt %d",
  554. cm->mport->id, slot, cm->tx_cnt);
  555. spin_lock(&cm->tx_lock);
  556. ack_slot = cm->tx_ack_slot;
  557. if (ack_slot == slot)
  558. riocm_debug(TX_EVENT, "slot == ack_slot");
  559. while (cm->tx_cnt && ((ack_slot != slot) ||
  560. (cm->tx_cnt == RIOCM_TX_RING_SIZE))) {
  561. cm->tx_buf[ack_slot] = NULL;
  562. ++ack_slot;
  563. ack_slot &= (RIOCM_TX_RING_SIZE - 1);
  564. cm->tx_cnt--;
  565. }
  566. if (cm->tx_cnt < 0 || cm->tx_cnt > RIOCM_TX_RING_SIZE)
  567. riocm_error("tx_cnt %d out of sync", cm->tx_cnt);
  568. WARN_ON((cm->tx_cnt < 0) || (cm->tx_cnt > RIOCM_TX_RING_SIZE));
  569. cm->tx_ack_slot = ack_slot;
  570. /*
  571. * If there are pending requests, insert them into transmit queue
  572. */
  573. if (!list_empty(&cm->tx_reqs) && (cm->tx_cnt < RIOCM_TX_RING_SIZE)) {
  574. struct tx_req *req, *_req;
  575. int rc;
  576. list_for_each_entry_safe(req, _req, &cm->tx_reqs, node) {
  577. list_del(&req->node);
  578. cm->tx_buf[cm->tx_slot] = req->buffer;
  579. rc = rio_add_outb_message(cm->mport, req->rdev, cmbox,
  580. req->buffer, req->len);
  581. kfree(req->buffer);
  582. kfree(req);
  583. ++cm->tx_cnt;
  584. ++cm->tx_slot;
  585. cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
  586. if (cm->tx_cnt == RIOCM_TX_RING_SIZE)
  587. break;
  588. }
  589. }
  590. spin_unlock(&cm->tx_lock);
  591. }
  592. static void riocm_outb_msg_event(struct rio_mport *mport, void *dev_id,
  593. int mbox, int slot)
  594. {
  595. struct cm_dev *cm = dev_id;
  596. if (cm && rio_mport_is_running(cm->mport))
  597. rio_txcq_handler(cm, slot);
  598. }
  599. static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev,
  600. void *buffer, size_t len)
  601. {
  602. unsigned long flags;
  603. struct tx_req *treq;
  604. treq = kzalloc(sizeof(*treq), GFP_KERNEL);
  605. if (treq == NULL)
  606. return -ENOMEM;
  607. treq->rdev = rdev;
  608. treq->buffer = buffer;
  609. treq->len = len;
  610. spin_lock_irqsave(&cm->tx_lock, flags);
  611. list_add_tail(&treq->node, &cm->tx_reqs);
  612. spin_unlock_irqrestore(&cm->tx_lock, flags);
  613. return 0;
  614. }
  615. /*
  616. * riocm_post_send - helper function that places packet into msg TX queue
  617. * @cm: cm_dev object
  618. * @rdev: target RapidIO device object (required by outbound msg interface)
  619. * @buffer: pointer to a packet buffer to send
  620. * @len: length of data to transfer
  621. * @req: request priority flag
  622. *
  623. * Returns: 0 if success, or error code otherwise.
  624. */
  625. static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
  626. void *buffer, size_t len)
  627. {
  628. int rc;
  629. unsigned long flags;
  630. spin_lock_irqsave(&cm->tx_lock, flags);
  631. if (cm->mport == NULL) {
  632. rc = -ENODEV;
  633. goto err_out;
  634. }
  635. if (cm->tx_cnt == RIOCM_TX_RING_SIZE) {
  636. riocm_debug(TX, "Tx Queue is full");
  637. rc = -EBUSY;
  638. goto err_out;
  639. }
  640. cm->tx_buf[cm->tx_slot] = buffer;
  641. rc = rio_add_outb_message(cm->mport, rdev, cmbox, buffer, len);
  642. riocm_debug(TX, "Add buf@%p destid=%x tx_slot=%d tx_cnt=%d",
  643. buffer, rdev->destid, cm->tx_slot, cm->tx_cnt);
  644. ++cm->tx_cnt;
  645. ++cm->tx_slot;
  646. cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
  647. err_out:
  648. spin_unlock_irqrestore(&cm->tx_lock, flags);
  649. return rc;
  650. }
  651. /*
  652. * riocm_ch_send - sends a data packet to a remote device
  653. * @ch_id: local channel ID
  654. * @buf: pointer to a data buffer to send (including CM header)
  655. * @len: length of data to transfer (including CM header)
  656. *
  657. * ATTN: ASSUMES THAT THE HEADER SPACE IS RESERVED PART OF THE DATA PACKET
  658. *
  659. * Returns: 0 if success, or
  660. * -EINVAL if one or more input parameters is/are not valid,
  661. * -ENODEV if cannot find a channel with specified ID,
  662. * -EAGAIN if a channel is not in CONNECTED state,
  663. * + error codes returned by HW send routine.
  664. */
  665. static int riocm_ch_send(u16 ch_id, void *buf, int len)
  666. {
  667. struct rio_channel *ch;
  668. struct rio_ch_chan_hdr *hdr;
  669. int ret;
  670. if (buf == NULL || ch_id == 0 || len == 0 || len > RIO_MAX_MSG_SIZE)
  671. return -EINVAL;
  672. ch = riocm_get_channel(ch_id);
  673. if (!ch) {
  674. riocm_error("%s(%d) ch_%d not found", current->comm,
  675. task_pid_nr(current), ch_id);
  676. return -ENODEV;
  677. }
  678. if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
  679. ret = -EAGAIN;
  680. goto err_out;
  681. }
  682. /*
  683. * Fill buffer header section with corresponding channel data
  684. */
  685. hdr = buf;
  686. hdr->bhdr.src_id = htonl(ch->loc_destid);
  687. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  688. hdr->bhdr.src_mbox = cmbox;
  689. hdr->bhdr.dst_mbox = cmbox;
  690. hdr->bhdr.type = RIO_CM_CHAN;
  691. hdr->ch_op = CM_DATA_MSG;
  692. hdr->dst_ch = htons(ch->rem_channel);
  693. hdr->src_ch = htons(ch->id);
  694. hdr->msg_len = htons((u16)len);
  695. /* ATTN: the function call below relies on the fact that underlying
  696. * HW-specific add_outb_message() routine copies TX data into its own
  697. * internal transfer buffer (true for all RIONET compatible mport
  698. * drivers). Must be reviewed if mport driver uses the buffer directly.
  699. */
  700. ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len);
  701. if (ret)
  702. riocm_debug(TX, "ch %d send_err=%d", ch->id, ret);
  703. err_out:
  704. riocm_put_channel(ch);
  705. return ret;
  706. }
  707. static int riocm_ch_free_rxbuf(struct rio_channel *ch, void *buf)
  708. {
  709. int i, ret = -EINVAL;
  710. spin_lock_bh(&ch->lock);
  711. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  712. if (ch->rx_ring.inuse[i] == buf) {
  713. ch->rx_ring.inuse[i] = NULL;
  714. ch->rx_ring.inuse_cnt--;
  715. ret = 0;
  716. break;
  717. }
  718. }
  719. spin_unlock_bh(&ch->lock);
  720. if (!ret)
  721. kfree(buf);
  722. return ret;
  723. }
  724. /*
  725. * riocm_ch_receive - fetch a data packet received for the specified channel
  726. * @ch: local channel ID
  727. * @buf: pointer to a packet buffer
  728. * @timeout: timeout to wait for incoming packet (in jiffies)
  729. *
  730. * Returns: 0 and valid buffer pointer if success, or NULL pointer and one of:
  731. * -EAGAIN if a channel is not in CONNECTED state,
  732. * -ENOMEM if in-use tracking queue is full,
  733. * -ETIME if wait timeout expired,
  734. * -EINTR if wait was interrupted.
  735. */
  736. static int riocm_ch_receive(struct rio_channel *ch, void **buf, long timeout)
  737. {
  738. void *rxmsg = NULL;
  739. int i, ret = 0;
  740. long wret;
  741. if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
  742. ret = -EAGAIN;
  743. goto out;
  744. }
  745. if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) {
  746. /* If we do not have entries to track buffers given to upper
  747. * layer, reject request.
  748. */
  749. ret = -ENOMEM;
  750. goto out;
  751. }
  752. wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout);
  753. riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret);
  754. if (!wret)
  755. ret = -ETIME;
  756. else if (wret == -ERESTARTSYS)
  757. ret = -EINTR;
  758. else
  759. ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET;
  760. if (ret)
  761. goto out;
  762. spin_lock_bh(&ch->lock);
  763. rxmsg = ch->rx_ring.buf[ch->rx_ring.tail];
  764. ch->rx_ring.buf[ch->rx_ring.tail] = NULL;
  765. ch->rx_ring.count--;
  766. ch->rx_ring.tail++;
  767. ch->rx_ring.tail %= RIOCM_RX_RING_SIZE;
  768. ret = -ENOMEM;
  769. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  770. if (ch->rx_ring.inuse[i] == NULL) {
  771. ch->rx_ring.inuse[i] = rxmsg;
  772. ch->rx_ring.inuse_cnt++;
  773. ret = 0;
  774. break;
  775. }
  776. }
  777. if (ret) {
  778. /* We have no entry to store pending message: drop it */
  779. kfree(rxmsg);
  780. rxmsg = NULL;
  781. }
  782. spin_unlock_bh(&ch->lock);
  783. out:
  784. *buf = rxmsg;
  785. return ret;
  786. }
  787. /*
  788. * riocm_ch_connect - sends a connect request to a remote device
  789. * @loc_ch: local channel ID
  790. * @cm: CM device to send connect request
  791. * @peer: target RapidIO device
  792. * @rem_ch: remote channel ID
  793. *
  794. * Returns: 0 if success, or
  795. * -EINVAL if the channel is not in IDLE state,
  796. * -EAGAIN if no connection request available immediately,
  797. * -ETIME if ACK response timeout expired,
  798. * -EINTR if wait for response was interrupted.
  799. */
  800. static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm,
  801. struct cm_peer *peer, u16 rem_ch)
  802. {
  803. struct rio_channel *ch = NULL;
  804. struct rio_ch_chan_hdr *hdr;
  805. int ret;
  806. long wret;
  807. ch = riocm_get_channel(loc_ch);
  808. if (!ch)
  809. return -ENODEV;
  810. if (!riocm_cmp_exch(ch, RIO_CM_IDLE, RIO_CM_CONNECT)) {
  811. ret = -EINVAL;
  812. goto conn_done;
  813. }
  814. ch->cmdev = cm;
  815. ch->rdev = peer->rdev;
  816. ch->context = NULL;
  817. ch->loc_destid = cm->mport->host_deviceid;
  818. ch->rem_channel = rem_ch;
  819. /*
  820. * Send connect request to the remote RapidIO device
  821. */
  822. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  823. if (hdr == NULL) {
  824. ret = -ENOMEM;
  825. goto conn_done;
  826. }
  827. hdr->bhdr.src_id = htonl(ch->loc_destid);
  828. hdr->bhdr.dst_id = htonl(peer->rdev->destid);
  829. hdr->bhdr.src_mbox = cmbox;
  830. hdr->bhdr.dst_mbox = cmbox;
  831. hdr->bhdr.type = RIO_CM_CHAN;
  832. hdr->ch_op = CM_CONN_REQ;
  833. hdr->dst_ch = htons(rem_ch);
  834. hdr->src_ch = htons(loc_ch);
  835. /* ATTN: the function call below relies on the fact that underlying
  836. * HW-specific add_outb_message() routine copies TX data into its
  837. * internal transfer buffer. Must be reviewed if mport driver uses
  838. * this buffer directly.
  839. */
  840. ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr));
  841. if (ret != -EBUSY) {
  842. kfree(hdr);
  843. } else {
  844. ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr));
  845. if (ret)
  846. kfree(hdr);
  847. }
  848. if (ret) {
  849. riocm_cmp_exch(ch, RIO_CM_CONNECT, RIO_CM_IDLE);
  850. goto conn_done;
  851. }
  852. /* Wait for connect response from the remote device */
  853. wret = wait_for_completion_interruptible_timeout(&ch->comp,
  854. RIOCM_CONNECT_TO * HZ);
  855. riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
  856. if (!wret)
  857. ret = -ETIME;
  858. else if (wret == -ERESTARTSYS)
  859. ret = -EINTR;
  860. else
  861. ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -1;
  862. conn_done:
  863. riocm_put_channel(ch);
  864. return ret;
  865. }
  866. static int riocm_send_ack(struct rio_channel *ch)
  867. {
  868. struct rio_ch_chan_hdr *hdr;
  869. int ret;
  870. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  871. if (hdr == NULL)
  872. return -ENOMEM;
  873. hdr->bhdr.src_id = htonl(ch->loc_destid);
  874. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  875. hdr->dst_ch = htons(ch->rem_channel);
  876. hdr->src_ch = htons(ch->id);
  877. hdr->bhdr.src_mbox = cmbox;
  878. hdr->bhdr.dst_mbox = cmbox;
  879. hdr->bhdr.type = RIO_CM_CHAN;
  880. hdr->ch_op = CM_CONN_ACK;
  881. /* ATTN: the function call below relies on the fact that underlying
  882. * add_outb_message() routine copies TX data into its internal transfer
  883. * buffer. Review if switching to direct buffer version.
  884. */
  885. ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
  886. if (ret == -EBUSY && !riocm_queue_req(ch->cmdev,
  887. ch->rdev, hdr, sizeof(*hdr)))
  888. return 0;
  889. kfree(hdr);
  890. if (ret)
  891. riocm_error("send ACK to ch_%d on %s failed (ret=%d)",
  892. ch->id, rio_name(ch->rdev), ret);
  893. return ret;
  894. }
  895. /*
  896. * riocm_ch_accept - accept incoming connection request
  897. * @ch_id: channel ID
  898. * @new_ch_id: local mport device
  899. * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection
  900. * request is not available).
  901. *
  902. * Returns: pointer to new channel struct if success, or error-valued pointer:
  903. * -ENODEV - cannot find specified channel or mport,
  904. * -EINVAL - the channel is not in IDLE state,
  905. * -EAGAIN - no connection request available immediately (timeout=0),
  906. * -ENOMEM - unable to allocate new channel,
  907. * -ETIME - wait timeout expired,
  908. * -EINTR - wait was interrupted.
  909. */
  910. static struct rio_channel *riocm_ch_accept(u16 ch_id, u16 *new_ch_id,
  911. long timeout)
  912. {
  913. struct rio_channel *ch;
  914. struct rio_channel *new_ch;
  915. struct conn_req *req;
  916. struct cm_peer *peer;
  917. int found = 0;
  918. int err = 0;
  919. long wret;
  920. ch = riocm_get_channel(ch_id);
  921. if (!ch)
  922. return ERR_PTR(-EINVAL);
  923. if (!riocm_cmp(ch, RIO_CM_LISTEN)) {
  924. err = -EINVAL;
  925. goto err_put;
  926. }
  927. /* Don't sleep if this is a non blocking call */
  928. if (!timeout) {
  929. if (!try_wait_for_completion(&ch->comp)) {
  930. err = -EAGAIN;
  931. goto err_put;
  932. }
  933. } else {
  934. riocm_debug(WAIT, "on %d", ch->id);
  935. wret = wait_for_completion_interruptible_timeout(&ch->comp,
  936. timeout);
  937. if (!wret) {
  938. err = -ETIME;
  939. goto err_put;
  940. } else if (wret == -ERESTARTSYS) {
  941. err = -EINTR;
  942. goto err_put;
  943. }
  944. }
  945. spin_lock_bh(&ch->lock);
  946. if (ch->state != RIO_CM_LISTEN) {
  947. err = -ECANCELED;
  948. } else if (list_empty(&ch->accept_queue)) {
  949. riocm_debug(WAIT, "on %d accept_queue is empty on completion",
  950. ch->id);
  951. err = -EIO;
  952. }
  953. spin_unlock_bh(&ch->lock);
  954. if (err) {
  955. riocm_debug(WAIT, "on %d returns %d", ch->id, err);
  956. goto err_put;
  957. }
  958. /* Create new channel for this connection */
  959. new_ch = riocm_ch_alloc(RIOCM_CHNUM_AUTO);
  960. if (IS_ERR(new_ch)) {
  961. riocm_error("failed to get channel for new req (%ld)",
  962. PTR_ERR(new_ch));
  963. err = -ENOMEM;
  964. goto err_put;
  965. }
  966. spin_lock_bh(&ch->lock);
  967. req = list_first_entry(&ch->accept_queue, struct conn_req, node);
  968. list_del(&req->node);
  969. new_ch->cmdev = ch->cmdev;
  970. new_ch->loc_destid = ch->loc_destid;
  971. new_ch->rem_destid = req->destid;
  972. new_ch->rem_channel = req->chan;
  973. spin_unlock_bh(&ch->lock);
  974. riocm_put_channel(ch);
  975. ch = NULL;
  976. kfree(req);
  977. down_read(&rdev_sem);
  978. /* Find requester's device object */
  979. list_for_each_entry(peer, &new_ch->cmdev->peers, node) {
  980. if (peer->rdev->destid == new_ch->rem_destid) {
  981. riocm_debug(RX_CMD, "found matching device(%s)",
  982. rio_name(peer->rdev));
  983. found = 1;
  984. break;
  985. }
  986. }
  987. up_read(&rdev_sem);
  988. if (!found) {
  989. /* If peer device object not found, simply ignore the request */
  990. err = -ENODEV;
  991. goto err_put_new_ch;
  992. }
  993. new_ch->rdev = peer->rdev;
  994. new_ch->state = RIO_CM_CONNECTED;
  995. spin_lock_init(&new_ch->lock);
  996. /* Acknowledge the connection request. */
  997. riocm_send_ack(new_ch);
  998. *new_ch_id = new_ch->id;
  999. return new_ch;
  1000. err_put_new_ch:
  1001. spin_lock_bh(&idr_lock);
  1002. idr_remove(&ch_idr, new_ch->id);
  1003. spin_unlock_bh(&idr_lock);
  1004. riocm_put_channel(new_ch);
  1005. err_put:
  1006. if (ch)
  1007. riocm_put_channel(ch);
  1008. *new_ch_id = 0;
  1009. return ERR_PTR(err);
  1010. }
  1011. /*
  1012. * riocm_ch_listen - puts a channel into LISTEN state
  1013. * @ch_id: channel ID
  1014. *
  1015. * Returns: 0 if success, or
  1016. * -EINVAL if the specified channel does not exists or
  1017. * is not in CHAN_BOUND state.
  1018. */
  1019. static int riocm_ch_listen(u16 ch_id)
  1020. {
  1021. struct rio_channel *ch = NULL;
  1022. int ret = 0;
  1023. riocm_debug(CHOP, "(ch_%d)", ch_id);
  1024. ch = riocm_get_channel(ch_id);
  1025. if (!ch || !riocm_cmp_exch(ch, RIO_CM_CHAN_BOUND, RIO_CM_LISTEN))
  1026. ret = -EINVAL;
  1027. riocm_put_channel(ch);
  1028. return ret;
  1029. }
  1030. /*
  1031. * riocm_ch_bind - associate a channel object and an mport device
  1032. * @ch_id: channel ID
  1033. * @mport_id: local mport device ID
  1034. * @context: pointer to the additional caller's context
  1035. *
  1036. * Returns: 0 if success, or
  1037. * -ENODEV if cannot find specified mport,
  1038. * -EINVAL if the specified channel does not exist or
  1039. * is not in IDLE state.
  1040. */
  1041. static int riocm_ch_bind(u16 ch_id, u8 mport_id, void *context)
  1042. {
  1043. struct rio_channel *ch = NULL;
  1044. struct cm_dev *cm;
  1045. int rc = -ENODEV;
  1046. riocm_debug(CHOP, "ch_%d to mport_%d", ch_id, mport_id);
  1047. /* Find matching cm_dev object */
  1048. down_read(&rdev_sem);
  1049. list_for_each_entry(cm, &cm_dev_list, list) {
  1050. if ((cm->mport->id == mport_id) &&
  1051. rio_mport_is_running(cm->mport)) {
  1052. rc = 0;
  1053. break;
  1054. }
  1055. }
  1056. if (rc)
  1057. goto exit;
  1058. ch = riocm_get_channel(ch_id);
  1059. if (!ch) {
  1060. rc = -EINVAL;
  1061. goto exit;
  1062. }
  1063. spin_lock_bh(&ch->lock);
  1064. if (ch->state != RIO_CM_IDLE) {
  1065. spin_unlock_bh(&ch->lock);
  1066. rc = -EINVAL;
  1067. goto err_put;
  1068. }
  1069. ch->cmdev = cm;
  1070. ch->loc_destid = cm->mport->host_deviceid;
  1071. ch->context = context;
  1072. ch->state = RIO_CM_CHAN_BOUND;
  1073. spin_unlock_bh(&ch->lock);
  1074. err_put:
  1075. riocm_put_channel(ch);
  1076. exit:
  1077. up_read(&rdev_sem);
  1078. return rc;
  1079. }
  1080. /*
  1081. * riocm_ch_alloc - channel object allocation helper routine
  1082. * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
  1083. *
  1084. * Return value: pointer to newly created channel object,
  1085. * or error-valued pointer
  1086. */
  1087. static struct rio_channel *riocm_ch_alloc(u16 ch_num)
  1088. {
  1089. int id;
  1090. int start, end;
  1091. struct rio_channel *ch;
  1092. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1093. if (!ch)
  1094. return ERR_PTR(-ENOMEM);
  1095. if (ch_num) {
  1096. /* If requested, try to obtain the specified channel ID */
  1097. start = ch_num;
  1098. end = ch_num + 1;
  1099. } else {
  1100. /* Obtain channel ID from the dynamic allocation range */
  1101. start = chstart;
  1102. end = RIOCM_MAX_CHNUM + 1;
  1103. }
  1104. idr_preload(GFP_KERNEL);
  1105. spin_lock_bh(&idr_lock);
  1106. id = idr_alloc_cyclic(&ch_idr, ch, start, end, GFP_NOWAIT);
  1107. spin_unlock_bh(&idr_lock);
  1108. idr_preload_end();
  1109. if (id < 0) {
  1110. kfree(ch);
  1111. return ERR_PTR(id == -ENOSPC ? -EBUSY : id);
  1112. }
  1113. ch->id = (u16)id;
  1114. ch->state = RIO_CM_IDLE;
  1115. spin_lock_init(&ch->lock);
  1116. INIT_LIST_HEAD(&ch->accept_queue);
  1117. INIT_LIST_HEAD(&ch->ch_node);
  1118. init_completion(&ch->comp);
  1119. init_completion(&ch->comp_close);
  1120. kref_init(&ch->ref);
  1121. ch->rx_ring.head = 0;
  1122. ch->rx_ring.tail = 0;
  1123. ch->rx_ring.count = 0;
  1124. ch->rx_ring.inuse_cnt = 0;
  1125. return ch;
  1126. }
  1127. /*
  1128. * riocm_ch_create - creates a new channel object and allocates ID for it
  1129. * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
  1130. *
  1131. * Allocates and initializes a new channel object. If the parameter ch_num > 0
  1132. * and is within the valid range, riocm_ch_create tries to allocate the
  1133. * specified ID for the new channel. If ch_num = 0, channel ID will be assigned
  1134. * automatically from the range (chstart ... RIOCM_MAX_CHNUM).
  1135. * Module parameter 'chstart' defines start of an ID range available for dynamic
  1136. * allocation. Range below 'chstart' is reserved for pre-defined ID numbers.
  1137. * Available channel numbers are limited by 16-bit size of channel numbers used
  1138. * in the packet header.
  1139. *
  1140. * Return value: PTR to rio_channel structure if successful (with channel number
  1141. * updated via pointer) or error-valued pointer if error.
  1142. */
  1143. static struct rio_channel *riocm_ch_create(u16 *ch_num)
  1144. {
  1145. struct rio_channel *ch = NULL;
  1146. ch = riocm_ch_alloc(*ch_num);
  1147. if (IS_ERR(ch))
  1148. riocm_debug(CHOP, "Failed to allocate channel %d (err=%ld)",
  1149. *ch_num, PTR_ERR(ch));
  1150. else
  1151. *ch_num = ch->id;
  1152. return ch;
  1153. }
  1154. /*
  1155. * riocm_ch_free - channel object release routine
  1156. * @ref: pointer to a channel's kref structure
  1157. */
  1158. static void riocm_ch_free(struct kref *ref)
  1159. {
  1160. struct rio_channel *ch = container_of(ref, struct rio_channel, ref);
  1161. int i;
  1162. riocm_debug(CHOP, "(ch_%d)", ch->id);
  1163. if (ch->rx_ring.inuse_cnt) {
  1164. for (i = 0;
  1165. i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) {
  1166. if (ch->rx_ring.inuse[i] != NULL) {
  1167. kfree(ch->rx_ring.inuse[i]);
  1168. ch->rx_ring.inuse_cnt--;
  1169. }
  1170. }
  1171. }
  1172. if (ch->rx_ring.count)
  1173. for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) {
  1174. if (ch->rx_ring.buf[i] != NULL) {
  1175. kfree(ch->rx_ring.buf[i]);
  1176. ch->rx_ring.count--;
  1177. }
  1178. }
  1179. complete(&ch->comp_close);
  1180. }
  1181. static int riocm_send_close(struct rio_channel *ch)
  1182. {
  1183. struct rio_ch_chan_hdr *hdr;
  1184. int ret;
  1185. /*
  1186. * Send CH_CLOSE notification to the remote RapidIO device
  1187. */
  1188. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  1189. if (hdr == NULL)
  1190. return -ENOMEM;
  1191. hdr->bhdr.src_id = htonl(ch->loc_destid);
  1192. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  1193. hdr->bhdr.src_mbox = cmbox;
  1194. hdr->bhdr.dst_mbox = cmbox;
  1195. hdr->bhdr.type = RIO_CM_CHAN;
  1196. hdr->ch_op = CM_CONN_CLOSE;
  1197. hdr->dst_ch = htons(ch->rem_channel);
  1198. hdr->src_ch = htons(ch->id);
  1199. /* ATTN: the function call below relies on the fact that underlying
  1200. * add_outb_message() routine copies TX data into its internal transfer
  1201. * buffer. Needs to be reviewed if switched to direct buffer mode.
  1202. */
  1203. ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
  1204. if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev,
  1205. hdr, sizeof(*hdr)))
  1206. return 0;
  1207. kfree(hdr);
  1208. if (ret)
  1209. riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret);
  1210. return ret;
  1211. }
  1212. /*
  1213. * riocm_ch_close - closes a channel object with specified ID (by local request)
  1214. * @ch: channel to be closed
  1215. */
  1216. static int riocm_ch_close(struct rio_channel *ch)
  1217. {
  1218. unsigned long tmo = msecs_to_jiffies(3000);
  1219. enum rio_cm_state state;
  1220. long wret;
  1221. int ret = 0;
  1222. riocm_debug(CHOP, "ch_%d by %s(%d)",
  1223. ch->id, current->comm, task_pid_nr(current));
  1224. state = riocm_exch(ch, RIO_CM_DESTROYING);
  1225. if (state == RIO_CM_CONNECTED)
  1226. riocm_send_close(ch);
  1227. complete_all(&ch->comp);
  1228. riocm_put_channel(ch);
  1229. wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo);
  1230. riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
  1231. if (wret == 0) {
  1232. /* Timeout on wait occurred */
  1233. riocm_debug(CHOP, "%s(%d) timed out waiting for ch %d",
  1234. current->comm, task_pid_nr(current), ch->id);
  1235. ret = -ETIMEDOUT;
  1236. } else if (wret == -ERESTARTSYS) {
  1237. /* Wait_for_completion was interrupted by a signal */
  1238. riocm_debug(CHOP, "%s(%d) wait for ch %d was interrupted",
  1239. current->comm, task_pid_nr(current), ch->id);
  1240. ret = -EINTR;
  1241. }
  1242. if (!ret) {
  1243. riocm_debug(CHOP, "ch_%d resources released", ch->id);
  1244. kfree(ch);
  1245. } else {
  1246. riocm_debug(CHOP, "failed to release ch_%d resources", ch->id);
  1247. }
  1248. return ret;
  1249. }
  1250. /*
  1251. * riocm_cdev_open() - Open character device
  1252. */
  1253. static int riocm_cdev_open(struct inode *inode, struct file *filp)
  1254. {
  1255. riocm_debug(INIT, "by %s(%d) filp=%p ",
  1256. current->comm, task_pid_nr(current), filp);
  1257. if (list_empty(&cm_dev_list))
  1258. return -ENODEV;
  1259. return 0;
  1260. }
  1261. /*
  1262. * riocm_cdev_release() - Release character device
  1263. */
  1264. static int riocm_cdev_release(struct inode *inode, struct file *filp)
  1265. {
  1266. struct rio_channel *ch, *_c;
  1267. unsigned int i;
  1268. LIST_HEAD(list);
  1269. riocm_debug(EXIT, "by %s(%d) filp=%p",
  1270. current->comm, task_pid_nr(current), filp);
  1271. /* Check if there are channels associated with this file descriptor */
  1272. spin_lock_bh(&idr_lock);
  1273. idr_for_each_entry(&ch_idr, ch, i) {
  1274. if (ch && ch->filp == filp) {
  1275. riocm_debug(EXIT, "ch_%d not released by %s(%d)",
  1276. ch->id, current->comm,
  1277. task_pid_nr(current));
  1278. idr_remove(&ch_idr, ch->id);
  1279. list_add(&ch->ch_node, &list);
  1280. }
  1281. }
  1282. spin_unlock_bh(&idr_lock);
  1283. if (!list_empty(&list)) {
  1284. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1285. list_del(&ch->ch_node);
  1286. riocm_ch_close(ch);
  1287. }
  1288. }
  1289. return 0;
  1290. }
  1291. /*
  1292. * cm_ep_get_list_size() - Reports number of endpoints in the network
  1293. */
  1294. static int cm_ep_get_list_size(void __user *arg)
  1295. {
  1296. u32 __user *p = arg;
  1297. u32 mport_id;
  1298. u32 count = 0;
  1299. struct cm_dev *cm;
  1300. if (get_user(mport_id, p))
  1301. return -EFAULT;
  1302. if (mport_id >= RIO_MAX_MPORTS)
  1303. return -EINVAL;
  1304. /* Find a matching cm_dev object */
  1305. down_read(&rdev_sem);
  1306. list_for_each_entry(cm, &cm_dev_list, list) {
  1307. if (cm->mport->id == mport_id) {
  1308. count = cm->npeers;
  1309. up_read(&rdev_sem);
  1310. if (copy_to_user(arg, &count, sizeof(u32)))
  1311. return -EFAULT;
  1312. return 0;
  1313. }
  1314. }
  1315. up_read(&rdev_sem);
  1316. return -ENODEV;
  1317. }
  1318. /*
  1319. * cm_ep_get_list() - Returns list of attached endpoints
  1320. */
  1321. static int cm_ep_get_list(void __user *arg)
  1322. {
  1323. struct cm_dev *cm;
  1324. struct cm_peer *peer;
  1325. u32 info[2];
  1326. void *buf;
  1327. u32 nent;
  1328. u32 *entry_ptr;
  1329. u32 i = 0;
  1330. int ret = 0;
  1331. if (copy_from_user(&info, arg, sizeof(info)))
  1332. return -EFAULT;
  1333. if (info[1] >= RIO_MAX_MPORTS || info[0] > RIOCM_MAX_EP_COUNT)
  1334. return -EINVAL;
  1335. /* Find a matching cm_dev object */
  1336. down_read(&rdev_sem);
  1337. list_for_each_entry(cm, &cm_dev_list, list)
  1338. if (cm->mport->id == (u8)info[1])
  1339. goto found;
  1340. up_read(&rdev_sem);
  1341. return -ENODEV;
  1342. found:
  1343. nent = min(info[0], cm->npeers);
  1344. buf = kcalloc(nent + 2, sizeof(u32), GFP_KERNEL);
  1345. if (!buf) {
  1346. up_read(&rdev_sem);
  1347. return -ENOMEM;
  1348. }
  1349. entry_ptr = (u32 *)((uintptr_t)buf + 2*sizeof(u32));
  1350. list_for_each_entry(peer, &cm->peers, node) {
  1351. *entry_ptr = (u32)peer->rdev->destid;
  1352. entry_ptr++;
  1353. if (++i == nent)
  1354. break;
  1355. }
  1356. up_read(&rdev_sem);
  1357. ((u32 *)buf)[0] = i; /* report an updated number of entries */
  1358. ((u32 *)buf)[1] = info[1]; /* put back an mport ID */
  1359. if (copy_to_user(arg, buf, sizeof(u32) * (info[0] + 2)))
  1360. ret = -EFAULT;
  1361. kfree(buf);
  1362. return ret;
  1363. }
  1364. /*
  1365. * cm_mport_get_list() - Returns list of available local mport devices
  1366. */
  1367. static int cm_mport_get_list(void __user *arg)
  1368. {
  1369. int ret = 0;
  1370. u32 entries;
  1371. void *buf;
  1372. struct cm_dev *cm;
  1373. u32 *entry_ptr;
  1374. int count = 0;
  1375. if (copy_from_user(&entries, arg, sizeof(entries)))
  1376. return -EFAULT;
  1377. if (entries == 0 || entries > RIO_MAX_MPORTS)
  1378. return -EINVAL;
  1379. buf = kcalloc(entries + 1, sizeof(u32), GFP_KERNEL);
  1380. if (!buf)
  1381. return -ENOMEM;
  1382. /* Scan all registered cm_dev objects */
  1383. entry_ptr = (u32 *)((uintptr_t)buf + sizeof(u32));
  1384. down_read(&rdev_sem);
  1385. list_for_each_entry(cm, &cm_dev_list, list) {
  1386. if (count++ < entries) {
  1387. *entry_ptr = (cm->mport->id << 16) |
  1388. cm->mport->host_deviceid;
  1389. entry_ptr++;
  1390. }
  1391. }
  1392. up_read(&rdev_sem);
  1393. *((u32 *)buf) = count; /* report a real number of entries */
  1394. if (copy_to_user(arg, buf, sizeof(u32) * (count + 1)))
  1395. ret = -EFAULT;
  1396. kfree(buf);
  1397. return ret;
  1398. }
  1399. /*
  1400. * cm_chan_create() - Create a message exchange channel
  1401. */
  1402. static int cm_chan_create(struct file *filp, void __user *arg)
  1403. {
  1404. u16 __user *p = arg;
  1405. u16 ch_num;
  1406. struct rio_channel *ch;
  1407. if (get_user(ch_num, p))
  1408. return -EFAULT;
  1409. riocm_debug(CHOP, "ch_%d requested by %s(%d)",
  1410. ch_num, current->comm, task_pid_nr(current));
  1411. ch = riocm_ch_create(&ch_num);
  1412. if (IS_ERR(ch))
  1413. return PTR_ERR(ch);
  1414. ch->filp = filp;
  1415. riocm_debug(CHOP, "ch_%d created by %s(%d)",
  1416. ch_num, current->comm, task_pid_nr(current));
  1417. return put_user(ch_num, p);
  1418. }
  1419. /*
  1420. * cm_chan_close() - Close channel
  1421. * @filp: Pointer to file object
  1422. * @arg: Channel to close
  1423. */
  1424. static int cm_chan_close(struct file *filp, void __user *arg)
  1425. {
  1426. u16 __user *p = arg;
  1427. u16 ch_num;
  1428. struct rio_channel *ch;
  1429. if (get_user(ch_num, p))
  1430. return -EFAULT;
  1431. riocm_debug(CHOP, "ch_%d by %s(%d)",
  1432. ch_num, current->comm, task_pid_nr(current));
  1433. spin_lock_bh(&idr_lock);
  1434. ch = idr_find(&ch_idr, ch_num);
  1435. if (!ch) {
  1436. spin_unlock_bh(&idr_lock);
  1437. return 0;
  1438. }
  1439. if (ch->filp != filp) {
  1440. spin_unlock_bh(&idr_lock);
  1441. return -EINVAL;
  1442. }
  1443. idr_remove(&ch_idr, ch->id);
  1444. spin_unlock_bh(&idr_lock);
  1445. return riocm_ch_close(ch);
  1446. }
  1447. /*
  1448. * cm_chan_bind() - Bind channel
  1449. * @arg: Channel number
  1450. */
  1451. static int cm_chan_bind(void __user *arg)
  1452. {
  1453. struct rio_cm_channel chan;
  1454. if (copy_from_user(&chan, arg, sizeof(chan)))
  1455. return -EFAULT;
  1456. if (chan.mport_id >= RIO_MAX_MPORTS)
  1457. return -EINVAL;
  1458. return riocm_ch_bind(chan.id, chan.mport_id, NULL);
  1459. }
  1460. /*
  1461. * cm_chan_listen() - Listen on channel
  1462. * @arg: Channel number
  1463. */
  1464. static int cm_chan_listen(void __user *arg)
  1465. {
  1466. u16 __user *p = arg;
  1467. u16 ch_num;
  1468. if (get_user(ch_num, p))
  1469. return -EFAULT;
  1470. return riocm_ch_listen(ch_num);
  1471. }
  1472. /*
  1473. * cm_chan_accept() - Accept incoming connection
  1474. * @filp: Pointer to file object
  1475. * @arg: Channel number
  1476. */
  1477. static int cm_chan_accept(struct file *filp, void __user *arg)
  1478. {
  1479. struct rio_cm_accept param;
  1480. long accept_to;
  1481. struct rio_channel *ch;
  1482. if (copy_from_user(&param, arg, sizeof(param)))
  1483. return -EFAULT;
  1484. riocm_debug(CHOP, "on ch_%d by %s(%d)",
  1485. param.ch_num, current->comm, task_pid_nr(current));
  1486. accept_to = param.wait_to ?
  1487. msecs_to_jiffies(param.wait_to) : 0;
  1488. ch = riocm_ch_accept(param.ch_num, &param.ch_num, accept_to);
  1489. if (IS_ERR(ch))
  1490. return PTR_ERR(ch);
  1491. ch->filp = filp;
  1492. riocm_debug(CHOP, "new ch_%d for %s(%d)",
  1493. ch->id, current->comm, task_pid_nr(current));
  1494. if (copy_to_user(arg, &param, sizeof(param)))
  1495. return -EFAULT;
  1496. return 0;
  1497. }
  1498. /*
  1499. * cm_chan_connect() - Connect on channel
  1500. * @arg: Channel information
  1501. */
  1502. static int cm_chan_connect(void __user *arg)
  1503. {
  1504. struct rio_cm_channel chan;
  1505. struct cm_dev *cm;
  1506. struct cm_peer *peer;
  1507. int ret = -ENODEV;
  1508. if (copy_from_user(&chan, arg, sizeof(chan)))
  1509. return -EFAULT;
  1510. if (chan.mport_id >= RIO_MAX_MPORTS)
  1511. return -EINVAL;
  1512. down_read(&rdev_sem);
  1513. /* Find matching cm_dev object */
  1514. list_for_each_entry(cm, &cm_dev_list, list) {
  1515. if (cm->mport->id == chan.mport_id) {
  1516. ret = 0;
  1517. break;
  1518. }
  1519. }
  1520. if (ret)
  1521. goto err_out;
  1522. if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) {
  1523. ret = -EINVAL;
  1524. goto err_out;
  1525. }
  1526. /* Find corresponding RapidIO endpoint device object */
  1527. ret = -ENODEV;
  1528. list_for_each_entry(peer, &cm->peers, node) {
  1529. if (peer->rdev->destid == chan.remote_destid) {
  1530. ret = 0;
  1531. break;
  1532. }
  1533. }
  1534. if (ret)
  1535. goto err_out;
  1536. up_read(&rdev_sem);
  1537. return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel);
  1538. err_out:
  1539. up_read(&rdev_sem);
  1540. return ret;
  1541. }
  1542. /*
  1543. * cm_chan_msg_send() - Send a message through channel
  1544. * @arg: Outbound message information
  1545. */
  1546. static int cm_chan_msg_send(void __user *arg)
  1547. {
  1548. struct rio_cm_msg msg;
  1549. void *buf;
  1550. int ret;
  1551. if (copy_from_user(&msg, arg, sizeof(msg)))
  1552. return -EFAULT;
  1553. if (msg.size > RIO_MAX_MSG_SIZE)
  1554. return -EINVAL;
  1555. buf = memdup_user((void __user *)(uintptr_t)msg.msg, msg.size);
  1556. if (IS_ERR(buf))
  1557. return PTR_ERR(buf);
  1558. ret = riocm_ch_send(msg.ch_num, buf, msg.size);
  1559. kfree(buf);
  1560. return ret;
  1561. }
  1562. /*
  1563. * cm_chan_msg_rcv() - Receive a message through channel
  1564. * @arg: Inbound message information
  1565. */
  1566. static int cm_chan_msg_rcv(void __user *arg)
  1567. {
  1568. struct rio_cm_msg msg;
  1569. struct rio_channel *ch;
  1570. void *buf;
  1571. long rxto;
  1572. int ret = 0, msg_size;
  1573. if (copy_from_user(&msg, arg, sizeof(msg)))
  1574. return -EFAULT;
  1575. if (msg.ch_num == 0 || msg.size == 0)
  1576. return -EINVAL;
  1577. ch = riocm_get_channel(msg.ch_num);
  1578. if (!ch)
  1579. return -ENODEV;
  1580. rxto = msg.rxto ? msecs_to_jiffies(msg.rxto) : MAX_SCHEDULE_TIMEOUT;
  1581. ret = riocm_ch_receive(ch, &buf, rxto);
  1582. if (ret)
  1583. goto out;
  1584. msg_size = min(msg.size, (u16)(RIO_MAX_MSG_SIZE));
  1585. if (copy_to_user((void __user *)(uintptr_t)msg.msg, buf, msg_size))
  1586. ret = -EFAULT;
  1587. riocm_ch_free_rxbuf(ch, buf);
  1588. out:
  1589. riocm_put_channel(ch);
  1590. return ret;
  1591. }
  1592. /*
  1593. * riocm_cdev_ioctl() - IOCTL requests handler
  1594. */
  1595. static long
  1596. riocm_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1597. {
  1598. switch (cmd) {
  1599. case RIO_CM_EP_GET_LIST_SIZE:
  1600. return cm_ep_get_list_size((void __user *)arg);
  1601. case RIO_CM_EP_GET_LIST:
  1602. return cm_ep_get_list((void __user *)arg);
  1603. case RIO_CM_CHAN_CREATE:
  1604. return cm_chan_create(filp, (void __user *)arg);
  1605. case RIO_CM_CHAN_CLOSE:
  1606. return cm_chan_close(filp, (void __user *)arg);
  1607. case RIO_CM_CHAN_BIND:
  1608. return cm_chan_bind((void __user *)arg);
  1609. case RIO_CM_CHAN_LISTEN:
  1610. return cm_chan_listen((void __user *)arg);
  1611. case RIO_CM_CHAN_ACCEPT:
  1612. return cm_chan_accept(filp, (void __user *)arg);
  1613. case RIO_CM_CHAN_CONNECT:
  1614. return cm_chan_connect((void __user *)arg);
  1615. case RIO_CM_CHAN_SEND:
  1616. return cm_chan_msg_send((void __user *)arg);
  1617. case RIO_CM_CHAN_RECEIVE:
  1618. return cm_chan_msg_rcv((void __user *)arg);
  1619. case RIO_CM_MPORT_GET_LIST:
  1620. return cm_mport_get_list((void __user *)arg);
  1621. default:
  1622. break;
  1623. }
  1624. return -EINVAL;
  1625. }
  1626. static const struct file_operations riocm_cdev_fops = {
  1627. .owner = THIS_MODULE,
  1628. .open = riocm_cdev_open,
  1629. .release = riocm_cdev_release,
  1630. .unlocked_ioctl = riocm_cdev_ioctl,
  1631. };
  1632. /*
  1633. * riocm_add_dev - add new remote RapidIO device into channel management core
  1634. * @dev: device object associated with RapidIO device
  1635. * @sif: subsystem interface
  1636. *
  1637. * Adds the specified RapidIO device (if applicable) into peers list of
  1638. * the corresponding channel management device (cm_dev).
  1639. */
  1640. static int riocm_add_dev(struct device *dev, struct subsys_interface *sif)
  1641. {
  1642. struct cm_peer *peer;
  1643. struct rio_dev *rdev = to_rio_dev(dev);
  1644. struct cm_dev *cm;
  1645. /* Check if the remote device has capabilities required to support CM */
  1646. if (!dev_cm_capable(rdev))
  1647. return 0;
  1648. riocm_debug(RDEV, "(%s)", rio_name(rdev));
  1649. peer = kmalloc(sizeof(*peer), GFP_KERNEL);
  1650. if (!peer)
  1651. return -ENOMEM;
  1652. /* Find a corresponding cm_dev object */
  1653. down_write(&rdev_sem);
  1654. list_for_each_entry(cm, &cm_dev_list, list) {
  1655. if (cm->mport == rdev->net->hport)
  1656. goto found;
  1657. }
  1658. up_write(&rdev_sem);
  1659. kfree(peer);
  1660. return -ENODEV;
  1661. found:
  1662. peer->rdev = rdev;
  1663. list_add_tail(&peer->node, &cm->peers);
  1664. cm->npeers++;
  1665. up_write(&rdev_sem);
  1666. return 0;
  1667. }
  1668. /*
  1669. * riocm_remove_dev - remove remote RapidIO device from channel management core
  1670. * @dev: device object associated with RapidIO device
  1671. * @sif: subsystem interface
  1672. *
  1673. * Removes the specified RapidIO device (if applicable) from peers list of
  1674. * the corresponding channel management device (cm_dev).
  1675. */
  1676. static void riocm_remove_dev(struct device *dev, struct subsys_interface *sif)
  1677. {
  1678. struct rio_dev *rdev = to_rio_dev(dev);
  1679. struct cm_dev *cm;
  1680. struct cm_peer *peer;
  1681. struct rio_channel *ch, *_c;
  1682. unsigned int i;
  1683. bool found = false;
  1684. LIST_HEAD(list);
  1685. /* Check if the remote device has capabilities required to support CM */
  1686. if (!dev_cm_capable(rdev))
  1687. return;
  1688. riocm_debug(RDEV, "(%s)", rio_name(rdev));
  1689. /* Find matching cm_dev object */
  1690. down_write(&rdev_sem);
  1691. list_for_each_entry(cm, &cm_dev_list, list) {
  1692. if (cm->mport == rdev->net->hport) {
  1693. found = true;
  1694. break;
  1695. }
  1696. }
  1697. if (!found) {
  1698. up_write(&rdev_sem);
  1699. return;
  1700. }
  1701. /* Remove remote device from the list of peers */
  1702. found = false;
  1703. list_for_each_entry(peer, &cm->peers, node) {
  1704. if (peer->rdev == rdev) {
  1705. riocm_debug(RDEV, "removing peer %s", rio_name(rdev));
  1706. found = true;
  1707. list_del(&peer->node);
  1708. cm->npeers--;
  1709. kfree(peer);
  1710. break;
  1711. }
  1712. }
  1713. up_write(&rdev_sem);
  1714. if (!found)
  1715. return;
  1716. /*
  1717. * Release channels associated with this peer
  1718. */
  1719. spin_lock_bh(&idr_lock);
  1720. idr_for_each_entry(&ch_idr, ch, i) {
  1721. if (ch && ch->rdev == rdev) {
  1722. if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN)
  1723. riocm_exch(ch, RIO_CM_DISCONNECT);
  1724. idr_remove(&ch_idr, ch->id);
  1725. list_add(&ch->ch_node, &list);
  1726. }
  1727. }
  1728. spin_unlock_bh(&idr_lock);
  1729. if (!list_empty(&list)) {
  1730. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1731. list_del(&ch->ch_node);
  1732. riocm_ch_close(ch);
  1733. }
  1734. }
  1735. }
  1736. /*
  1737. * riocm_cdev_add() - Create rio_cm char device
  1738. * @devno: device number assigned to device (MAJ + MIN)
  1739. */
  1740. static int riocm_cdev_add(dev_t devno)
  1741. {
  1742. int ret;
  1743. cdev_init(&riocm_cdev.cdev, &riocm_cdev_fops);
  1744. riocm_cdev.cdev.owner = THIS_MODULE;
  1745. ret = cdev_add(&riocm_cdev.cdev, devno, 1);
  1746. if (ret < 0) {
  1747. riocm_error("Cannot register a device with error %d", ret);
  1748. return ret;
  1749. }
  1750. riocm_cdev.dev = device_create(dev_class, NULL, devno, NULL, DEV_NAME);
  1751. if (IS_ERR(riocm_cdev.dev)) {
  1752. cdev_del(&riocm_cdev.cdev);
  1753. return PTR_ERR(riocm_cdev.dev);
  1754. }
  1755. riocm_debug(MPORT, "Added %s cdev(%d:%d)",
  1756. DEV_NAME, MAJOR(devno), MINOR(devno));
  1757. return 0;
  1758. }
  1759. /*
  1760. * riocm_add_mport - add new local mport device into channel management core
  1761. * @dev: device object associated with mport
  1762. * @class_intf: class interface
  1763. *
  1764. * When a new mport device is added, CM immediately reserves inbound and
  1765. * outbound RapidIO mailboxes that will be used.
  1766. */
  1767. static int riocm_add_mport(struct device *dev,
  1768. struct class_interface *class_intf)
  1769. {
  1770. int rc;
  1771. int i;
  1772. struct cm_dev *cm;
  1773. struct rio_mport *mport = to_rio_mport(dev);
  1774. riocm_debug(MPORT, "add mport %s", mport->name);
  1775. cm = kzalloc(sizeof(*cm), GFP_KERNEL);
  1776. if (!cm)
  1777. return -ENOMEM;
  1778. cm->mport = mport;
  1779. rc = rio_request_outb_mbox(mport, cm, cmbox,
  1780. RIOCM_TX_RING_SIZE, riocm_outb_msg_event);
  1781. if (rc) {
  1782. riocm_error("failed to allocate OBMBOX_%d on %s",
  1783. cmbox, mport->name);
  1784. kfree(cm);
  1785. return -ENODEV;
  1786. }
  1787. rc = rio_request_inb_mbox(mport, cm, cmbox,
  1788. RIOCM_RX_RING_SIZE, riocm_inb_msg_event);
  1789. if (rc) {
  1790. riocm_error("failed to allocate IBMBOX_%d on %s",
  1791. cmbox, mport->name);
  1792. rio_release_outb_mbox(mport, cmbox);
  1793. kfree(cm);
  1794. return -ENODEV;
  1795. }
  1796. /*
  1797. * Allocate and register inbound messaging buffers to be ready
  1798. * to receive channel and system management requests
  1799. */
  1800. for (i = 0; i < RIOCM_RX_RING_SIZE; i++)
  1801. cm->rx_buf[i] = NULL;
  1802. cm->rx_slots = RIOCM_RX_RING_SIZE;
  1803. mutex_init(&cm->rx_lock);
  1804. riocm_rx_fill(cm, RIOCM_RX_RING_SIZE);
  1805. cm->rx_wq = create_workqueue(DRV_NAME "/rxq");
  1806. if (!cm->rx_wq) {
  1807. riocm_error("failed to allocate IBMBOX_%d on %s",
  1808. cmbox, mport->name);
  1809. rio_release_outb_mbox(mport, cmbox);
  1810. kfree(cm);
  1811. return -ENOMEM;
  1812. }
  1813. INIT_WORK(&cm->rx_work, rio_ibmsg_handler);
  1814. cm->tx_slot = 0;
  1815. cm->tx_cnt = 0;
  1816. cm->tx_ack_slot = 0;
  1817. spin_lock_init(&cm->tx_lock);
  1818. INIT_LIST_HEAD(&cm->peers);
  1819. cm->npeers = 0;
  1820. INIT_LIST_HEAD(&cm->tx_reqs);
  1821. down_write(&rdev_sem);
  1822. list_add_tail(&cm->list, &cm_dev_list);
  1823. up_write(&rdev_sem);
  1824. return 0;
  1825. }
  1826. /*
  1827. * riocm_remove_mport - remove local mport device from channel management core
  1828. * @dev: device object associated with mport
  1829. * @class_intf: class interface
  1830. *
  1831. * Removes a local mport device from the list of registered devices that provide
  1832. * channel management services. Returns an error if the specified mport is not
  1833. * registered with the CM core.
  1834. */
  1835. static void riocm_remove_mport(struct device *dev,
  1836. struct class_interface *class_intf)
  1837. {
  1838. struct rio_mport *mport = to_rio_mport(dev);
  1839. struct cm_dev *cm;
  1840. struct cm_peer *peer, *temp;
  1841. struct rio_channel *ch, *_c;
  1842. unsigned int i;
  1843. bool found = false;
  1844. LIST_HEAD(list);
  1845. riocm_debug(MPORT, "%s", mport->name);
  1846. /* Find a matching cm_dev object */
  1847. down_write(&rdev_sem);
  1848. list_for_each_entry(cm, &cm_dev_list, list) {
  1849. if (cm->mport == mport) {
  1850. list_del(&cm->list);
  1851. found = true;
  1852. break;
  1853. }
  1854. }
  1855. up_write(&rdev_sem);
  1856. if (!found)
  1857. return;
  1858. flush_workqueue(cm->rx_wq);
  1859. destroy_workqueue(cm->rx_wq);
  1860. /* Release channels bound to this mport */
  1861. spin_lock_bh(&idr_lock);
  1862. idr_for_each_entry(&ch_idr, ch, i) {
  1863. if (ch->cmdev == cm) {
  1864. riocm_debug(RDEV, "%s drop ch_%d",
  1865. mport->name, ch->id);
  1866. idr_remove(&ch_idr, ch->id);
  1867. list_add(&ch->ch_node, &list);
  1868. }
  1869. }
  1870. spin_unlock_bh(&idr_lock);
  1871. if (!list_empty(&list)) {
  1872. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1873. list_del(&ch->ch_node);
  1874. riocm_ch_close(ch);
  1875. }
  1876. }
  1877. rio_release_inb_mbox(mport, cmbox);
  1878. rio_release_outb_mbox(mport, cmbox);
  1879. /* Remove and free peer entries */
  1880. if (!list_empty(&cm->peers))
  1881. riocm_debug(RDEV, "ATTN: peer list not empty");
  1882. list_for_each_entry_safe(peer, temp, &cm->peers, node) {
  1883. riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev));
  1884. list_del(&peer->node);
  1885. kfree(peer);
  1886. }
  1887. riocm_rx_free(cm);
  1888. kfree(cm);
  1889. riocm_debug(MPORT, "%s done", mport->name);
  1890. }
  1891. static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
  1892. void *unused)
  1893. {
  1894. struct rio_channel *ch;
  1895. unsigned int i;
  1896. LIST_HEAD(list);
  1897. riocm_debug(EXIT, ".");
  1898. /*
  1899. * If there are any channels left in connected state send
  1900. * close notification to the connection partner.
  1901. * First build a list of channels that require a closing
  1902. * notification because function riocm_send_close() should
  1903. * be called outside of spinlock protected code.
  1904. */
  1905. spin_lock_bh(&idr_lock);
  1906. idr_for_each_entry(&ch_idr, ch, i) {
  1907. if (ch->state == RIO_CM_CONNECTED) {
  1908. riocm_debug(EXIT, "close ch %d", ch->id);
  1909. idr_remove(&ch_idr, ch->id);
  1910. list_add(&ch->ch_node, &list);
  1911. }
  1912. }
  1913. spin_unlock_bh(&idr_lock);
  1914. list_for_each_entry(ch, &list, ch_node)
  1915. riocm_send_close(ch);
  1916. return NOTIFY_DONE;
  1917. }
  1918. /*
  1919. * riocm_interface handles addition/removal of remote RapidIO devices
  1920. */
  1921. static struct subsys_interface riocm_interface = {
  1922. .name = "rio_cm",
  1923. .subsys = &rio_bus_type,
  1924. .add_dev = riocm_add_dev,
  1925. .remove_dev = riocm_remove_dev,
  1926. };
  1927. /*
  1928. * rio_mport_interface handles addition/removal local mport devices
  1929. */
  1930. static struct class_interface rio_mport_interface __refdata = {
  1931. .class = &rio_mport_class,
  1932. .add_dev = riocm_add_mport,
  1933. .remove_dev = riocm_remove_mport,
  1934. };
  1935. static struct notifier_block rio_cm_notifier = {
  1936. .notifier_call = rio_cm_shutdown,
  1937. };
  1938. static int __init riocm_init(void)
  1939. {
  1940. int ret;
  1941. /* Create device class needed by udev */
  1942. dev_class = class_create(THIS_MODULE, DRV_NAME);
  1943. if (IS_ERR(dev_class)) {
  1944. riocm_error("Cannot create " DRV_NAME " class");
  1945. return PTR_ERR(dev_class);
  1946. }
  1947. ret = alloc_chrdev_region(&dev_number, 0, 1, DRV_NAME);
  1948. if (ret) {
  1949. class_destroy(dev_class);
  1950. return ret;
  1951. }
  1952. dev_major = MAJOR(dev_number);
  1953. dev_minor_base = MINOR(dev_number);
  1954. riocm_debug(INIT, "Registered class with %d major", dev_major);
  1955. /*
  1956. * Register as rapidio_port class interface to get notifications about
  1957. * mport additions and removals.
  1958. */
  1959. ret = class_interface_register(&rio_mport_interface);
  1960. if (ret) {
  1961. riocm_error("class_interface_register error: %d", ret);
  1962. goto err_reg;
  1963. }
  1964. /*
  1965. * Register as RapidIO bus interface to get notifications about
  1966. * addition/removal of remote RapidIO devices.
  1967. */
  1968. ret = subsys_interface_register(&riocm_interface);
  1969. if (ret) {
  1970. riocm_error("subsys_interface_register error: %d", ret);
  1971. goto err_cl;
  1972. }
  1973. ret = register_reboot_notifier(&rio_cm_notifier);
  1974. if (ret) {
  1975. riocm_error("failed to register reboot notifier (err=%d)", ret);
  1976. goto err_sif;
  1977. }
  1978. ret = riocm_cdev_add(dev_number);
  1979. if (ret) {
  1980. unregister_reboot_notifier(&rio_cm_notifier);
  1981. ret = -ENODEV;
  1982. goto err_sif;
  1983. }
  1984. return 0;
  1985. err_sif:
  1986. subsys_interface_unregister(&riocm_interface);
  1987. err_cl:
  1988. class_interface_unregister(&rio_mport_interface);
  1989. err_reg:
  1990. unregister_chrdev_region(dev_number, 1);
  1991. class_destroy(dev_class);
  1992. return ret;
  1993. }
  1994. static void __exit riocm_exit(void)
  1995. {
  1996. riocm_debug(EXIT, "enter");
  1997. unregister_reboot_notifier(&rio_cm_notifier);
  1998. subsys_interface_unregister(&riocm_interface);
  1999. class_interface_unregister(&rio_mport_interface);
  2000. idr_destroy(&ch_idr);
  2001. device_unregister(riocm_cdev.dev);
  2002. cdev_del(&(riocm_cdev.cdev));
  2003. class_destroy(dev_class);
  2004. unregister_chrdev_region(dev_number, 1);
  2005. }
  2006. late_initcall(riocm_init);
  2007. module_exit(riocm_exit);