rio_cm.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  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)
  1026. return -EINVAL;
  1027. if (!riocm_cmp_exch(ch, RIO_CM_CHAN_BOUND, RIO_CM_LISTEN))
  1028. ret = -EINVAL;
  1029. riocm_put_channel(ch);
  1030. return ret;
  1031. }
  1032. /*
  1033. * riocm_ch_bind - associate a channel object and an mport device
  1034. * @ch_id: channel ID
  1035. * @mport_id: local mport device ID
  1036. * @context: pointer to the additional caller's context
  1037. *
  1038. * Returns: 0 if success, or
  1039. * -ENODEV if cannot find specified mport,
  1040. * -EINVAL if the specified channel does not exist or
  1041. * is not in IDLE state.
  1042. */
  1043. static int riocm_ch_bind(u16 ch_id, u8 mport_id, void *context)
  1044. {
  1045. struct rio_channel *ch = NULL;
  1046. struct cm_dev *cm;
  1047. int rc = -ENODEV;
  1048. riocm_debug(CHOP, "ch_%d to mport_%d", ch_id, mport_id);
  1049. /* Find matching cm_dev object */
  1050. down_read(&rdev_sem);
  1051. list_for_each_entry(cm, &cm_dev_list, list) {
  1052. if ((cm->mport->id == mport_id) &&
  1053. rio_mport_is_running(cm->mport)) {
  1054. rc = 0;
  1055. break;
  1056. }
  1057. }
  1058. if (rc)
  1059. goto exit;
  1060. ch = riocm_get_channel(ch_id);
  1061. if (!ch) {
  1062. rc = -EINVAL;
  1063. goto exit;
  1064. }
  1065. spin_lock_bh(&ch->lock);
  1066. if (ch->state != RIO_CM_IDLE) {
  1067. spin_unlock_bh(&ch->lock);
  1068. rc = -EINVAL;
  1069. goto err_put;
  1070. }
  1071. ch->cmdev = cm;
  1072. ch->loc_destid = cm->mport->host_deviceid;
  1073. ch->context = context;
  1074. ch->state = RIO_CM_CHAN_BOUND;
  1075. spin_unlock_bh(&ch->lock);
  1076. err_put:
  1077. riocm_put_channel(ch);
  1078. exit:
  1079. up_read(&rdev_sem);
  1080. return rc;
  1081. }
  1082. /*
  1083. * riocm_ch_alloc - channel object allocation helper routine
  1084. * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
  1085. *
  1086. * Return value: pointer to newly created channel object,
  1087. * or error-valued pointer
  1088. */
  1089. static struct rio_channel *riocm_ch_alloc(u16 ch_num)
  1090. {
  1091. int id;
  1092. int start, end;
  1093. struct rio_channel *ch;
  1094. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1095. if (!ch)
  1096. return ERR_PTR(-ENOMEM);
  1097. if (ch_num) {
  1098. /* If requested, try to obtain the specified channel ID */
  1099. start = ch_num;
  1100. end = ch_num + 1;
  1101. } else {
  1102. /* Obtain channel ID from the dynamic allocation range */
  1103. start = chstart;
  1104. end = RIOCM_MAX_CHNUM + 1;
  1105. }
  1106. idr_preload(GFP_KERNEL);
  1107. spin_lock_bh(&idr_lock);
  1108. id = idr_alloc_cyclic(&ch_idr, ch, start, end, GFP_NOWAIT);
  1109. spin_unlock_bh(&idr_lock);
  1110. idr_preload_end();
  1111. if (id < 0) {
  1112. kfree(ch);
  1113. return ERR_PTR(id == -ENOSPC ? -EBUSY : id);
  1114. }
  1115. ch->id = (u16)id;
  1116. ch->state = RIO_CM_IDLE;
  1117. spin_lock_init(&ch->lock);
  1118. INIT_LIST_HEAD(&ch->accept_queue);
  1119. INIT_LIST_HEAD(&ch->ch_node);
  1120. init_completion(&ch->comp);
  1121. init_completion(&ch->comp_close);
  1122. kref_init(&ch->ref);
  1123. ch->rx_ring.head = 0;
  1124. ch->rx_ring.tail = 0;
  1125. ch->rx_ring.count = 0;
  1126. ch->rx_ring.inuse_cnt = 0;
  1127. return ch;
  1128. }
  1129. /*
  1130. * riocm_ch_create - creates a new channel object and allocates ID for it
  1131. * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
  1132. *
  1133. * Allocates and initializes a new channel object. If the parameter ch_num > 0
  1134. * and is within the valid range, riocm_ch_create tries to allocate the
  1135. * specified ID for the new channel. If ch_num = 0, channel ID will be assigned
  1136. * automatically from the range (chstart ... RIOCM_MAX_CHNUM).
  1137. * Module parameter 'chstart' defines start of an ID range available for dynamic
  1138. * allocation. Range below 'chstart' is reserved for pre-defined ID numbers.
  1139. * Available channel numbers are limited by 16-bit size of channel numbers used
  1140. * in the packet header.
  1141. *
  1142. * Return value: PTR to rio_channel structure if successful (with channel number
  1143. * updated via pointer) or error-valued pointer if error.
  1144. */
  1145. static struct rio_channel *riocm_ch_create(u16 *ch_num)
  1146. {
  1147. struct rio_channel *ch = NULL;
  1148. ch = riocm_ch_alloc(*ch_num);
  1149. if (IS_ERR(ch))
  1150. riocm_debug(CHOP, "Failed to allocate channel %d (err=%ld)",
  1151. *ch_num, PTR_ERR(ch));
  1152. else
  1153. *ch_num = ch->id;
  1154. return ch;
  1155. }
  1156. /*
  1157. * riocm_ch_free - channel object release routine
  1158. * @ref: pointer to a channel's kref structure
  1159. */
  1160. static void riocm_ch_free(struct kref *ref)
  1161. {
  1162. struct rio_channel *ch = container_of(ref, struct rio_channel, ref);
  1163. int i;
  1164. riocm_debug(CHOP, "(ch_%d)", ch->id);
  1165. if (ch->rx_ring.inuse_cnt) {
  1166. for (i = 0;
  1167. i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) {
  1168. if (ch->rx_ring.inuse[i] != NULL) {
  1169. kfree(ch->rx_ring.inuse[i]);
  1170. ch->rx_ring.inuse_cnt--;
  1171. }
  1172. }
  1173. }
  1174. if (ch->rx_ring.count)
  1175. for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) {
  1176. if (ch->rx_ring.buf[i] != NULL) {
  1177. kfree(ch->rx_ring.buf[i]);
  1178. ch->rx_ring.count--;
  1179. }
  1180. }
  1181. complete(&ch->comp_close);
  1182. }
  1183. static int riocm_send_close(struct rio_channel *ch)
  1184. {
  1185. struct rio_ch_chan_hdr *hdr;
  1186. int ret;
  1187. /*
  1188. * Send CH_CLOSE notification to the remote RapidIO device
  1189. */
  1190. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  1191. if (hdr == NULL)
  1192. return -ENOMEM;
  1193. hdr->bhdr.src_id = htonl(ch->loc_destid);
  1194. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  1195. hdr->bhdr.src_mbox = cmbox;
  1196. hdr->bhdr.dst_mbox = cmbox;
  1197. hdr->bhdr.type = RIO_CM_CHAN;
  1198. hdr->ch_op = CM_CONN_CLOSE;
  1199. hdr->dst_ch = htons(ch->rem_channel);
  1200. hdr->src_ch = htons(ch->id);
  1201. /* ATTN: the function call below relies on the fact that underlying
  1202. * add_outb_message() routine copies TX data into its internal transfer
  1203. * buffer. Needs to be reviewed if switched to direct buffer mode.
  1204. */
  1205. ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
  1206. if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev,
  1207. hdr, sizeof(*hdr)))
  1208. return 0;
  1209. kfree(hdr);
  1210. if (ret)
  1211. riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret);
  1212. return ret;
  1213. }
  1214. /*
  1215. * riocm_ch_close - closes a channel object with specified ID (by local request)
  1216. * @ch: channel to be closed
  1217. */
  1218. static int riocm_ch_close(struct rio_channel *ch)
  1219. {
  1220. unsigned long tmo = msecs_to_jiffies(3000);
  1221. enum rio_cm_state state;
  1222. long wret;
  1223. int ret = 0;
  1224. riocm_debug(CHOP, "ch_%d by %s(%d)",
  1225. ch->id, current->comm, task_pid_nr(current));
  1226. state = riocm_exch(ch, RIO_CM_DESTROYING);
  1227. if (state == RIO_CM_CONNECTED)
  1228. riocm_send_close(ch);
  1229. complete_all(&ch->comp);
  1230. riocm_put_channel(ch);
  1231. wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo);
  1232. riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
  1233. if (wret == 0) {
  1234. /* Timeout on wait occurred */
  1235. riocm_debug(CHOP, "%s(%d) timed out waiting for ch %d",
  1236. current->comm, task_pid_nr(current), ch->id);
  1237. ret = -ETIMEDOUT;
  1238. } else if (wret == -ERESTARTSYS) {
  1239. /* Wait_for_completion was interrupted by a signal */
  1240. riocm_debug(CHOP, "%s(%d) wait for ch %d was interrupted",
  1241. current->comm, task_pid_nr(current), ch->id);
  1242. ret = -EINTR;
  1243. }
  1244. if (!ret) {
  1245. riocm_debug(CHOP, "ch_%d resources released", ch->id);
  1246. kfree(ch);
  1247. } else {
  1248. riocm_debug(CHOP, "failed to release ch_%d resources", ch->id);
  1249. }
  1250. return ret;
  1251. }
  1252. /*
  1253. * riocm_cdev_open() - Open character device
  1254. */
  1255. static int riocm_cdev_open(struct inode *inode, struct file *filp)
  1256. {
  1257. riocm_debug(INIT, "by %s(%d) filp=%p ",
  1258. current->comm, task_pid_nr(current), filp);
  1259. if (list_empty(&cm_dev_list))
  1260. return -ENODEV;
  1261. return 0;
  1262. }
  1263. /*
  1264. * riocm_cdev_release() - Release character device
  1265. */
  1266. static int riocm_cdev_release(struct inode *inode, struct file *filp)
  1267. {
  1268. struct rio_channel *ch, *_c;
  1269. unsigned int i;
  1270. LIST_HEAD(list);
  1271. riocm_debug(EXIT, "by %s(%d) filp=%p",
  1272. current->comm, task_pid_nr(current), filp);
  1273. /* Check if there are channels associated with this file descriptor */
  1274. spin_lock_bh(&idr_lock);
  1275. idr_for_each_entry(&ch_idr, ch, i) {
  1276. if (ch && ch->filp == filp) {
  1277. riocm_debug(EXIT, "ch_%d not released by %s(%d)",
  1278. ch->id, current->comm,
  1279. task_pid_nr(current));
  1280. idr_remove(&ch_idr, ch->id);
  1281. list_add(&ch->ch_node, &list);
  1282. }
  1283. }
  1284. spin_unlock_bh(&idr_lock);
  1285. if (!list_empty(&list)) {
  1286. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1287. list_del(&ch->ch_node);
  1288. riocm_ch_close(ch);
  1289. }
  1290. }
  1291. return 0;
  1292. }
  1293. /*
  1294. * cm_ep_get_list_size() - Reports number of endpoints in the network
  1295. */
  1296. static int cm_ep_get_list_size(void __user *arg)
  1297. {
  1298. u32 __user *p = arg;
  1299. u32 mport_id;
  1300. u32 count = 0;
  1301. struct cm_dev *cm;
  1302. if (get_user(mport_id, p))
  1303. return -EFAULT;
  1304. if (mport_id >= RIO_MAX_MPORTS)
  1305. return -EINVAL;
  1306. /* Find a matching cm_dev object */
  1307. down_read(&rdev_sem);
  1308. list_for_each_entry(cm, &cm_dev_list, list) {
  1309. if (cm->mport->id == mport_id) {
  1310. count = cm->npeers;
  1311. up_read(&rdev_sem);
  1312. if (copy_to_user(arg, &count, sizeof(u32)))
  1313. return -EFAULT;
  1314. return 0;
  1315. }
  1316. }
  1317. up_read(&rdev_sem);
  1318. return -ENODEV;
  1319. }
  1320. /*
  1321. * cm_ep_get_list() - Returns list of attached endpoints
  1322. */
  1323. static int cm_ep_get_list(void __user *arg)
  1324. {
  1325. struct cm_dev *cm;
  1326. struct cm_peer *peer;
  1327. u32 info[2];
  1328. void *buf;
  1329. u32 nent;
  1330. u32 *entry_ptr;
  1331. u32 i = 0;
  1332. int ret = 0;
  1333. if (copy_from_user(&info, arg, sizeof(info)))
  1334. return -EFAULT;
  1335. if (info[1] >= RIO_MAX_MPORTS || info[0] > RIOCM_MAX_EP_COUNT)
  1336. return -EINVAL;
  1337. /* Find a matching cm_dev object */
  1338. down_read(&rdev_sem);
  1339. list_for_each_entry(cm, &cm_dev_list, list)
  1340. if (cm->mport->id == (u8)info[1])
  1341. goto found;
  1342. up_read(&rdev_sem);
  1343. return -ENODEV;
  1344. found:
  1345. nent = min(info[0], cm->npeers);
  1346. buf = kcalloc(nent + 2, sizeof(u32), GFP_KERNEL);
  1347. if (!buf) {
  1348. up_read(&rdev_sem);
  1349. return -ENOMEM;
  1350. }
  1351. entry_ptr = (u32 *)((uintptr_t)buf + 2*sizeof(u32));
  1352. list_for_each_entry(peer, &cm->peers, node) {
  1353. *entry_ptr = (u32)peer->rdev->destid;
  1354. entry_ptr++;
  1355. if (++i == nent)
  1356. break;
  1357. }
  1358. up_read(&rdev_sem);
  1359. ((u32 *)buf)[0] = i; /* report an updated number of entries */
  1360. ((u32 *)buf)[1] = info[1]; /* put back an mport ID */
  1361. if (copy_to_user(arg, buf, sizeof(u32) * (info[0] + 2)))
  1362. ret = -EFAULT;
  1363. kfree(buf);
  1364. return ret;
  1365. }
  1366. /*
  1367. * cm_mport_get_list() - Returns list of available local mport devices
  1368. */
  1369. static int cm_mport_get_list(void __user *arg)
  1370. {
  1371. int ret = 0;
  1372. u32 entries;
  1373. void *buf;
  1374. struct cm_dev *cm;
  1375. u32 *entry_ptr;
  1376. int count = 0;
  1377. if (copy_from_user(&entries, arg, sizeof(entries)))
  1378. return -EFAULT;
  1379. if (entries == 0 || entries > RIO_MAX_MPORTS)
  1380. return -EINVAL;
  1381. buf = kcalloc(entries + 1, sizeof(u32), GFP_KERNEL);
  1382. if (!buf)
  1383. return -ENOMEM;
  1384. /* Scan all registered cm_dev objects */
  1385. entry_ptr = (u32 *)((uintptr_t)buf + sizeof(u32));
  1386. down_read(&rdev_sem);
  1387. list_for_each_entry(cm, &cm_dev_list, list) {
  1388. if (count++ < entries) {
  1389. *entry_ptr = (cm->mport->id << 16) |
  1390. cm->mport->host_deviceid;
  1391. entry_ptr++;
  1392. }
  1393. }
  1394. up_read(&rdev_sem);
  1395. *((u32 *)buf) = count; /* report a real number of entries */
  1396. if (copy_to_user(arg, buf, sizeof(u32) * (count + 1)))
  1397. ret = -EFAULT;
  1398. kfree(buf);
  1399. return ret;
  1400. }
  1401. /*
  1402. * cm_chan_create() - Create a message exchange channel
  1403. */
  1404. static int cm_chan_create(struct file *filp, void __user *arg)
  1405. {
  1406. u16 __user *p = arg;
  1407. u16 ch_num;
  1408. struct rio_channel *ch;
  1409. if (get_user(ch_num, p))
  1410. return -EFAULT;
  1411. riocm_debug(CHOP, "ch_%d requested by %s(%d)",
  1412. ch_num, current->comm, task_pid_nr(current));
  1413. ch = riocm_ch_create(&ch_num);
  1414. if (IS_ERR(ch))
  1415. return PTR_ERR(ch);
  1416. ch->filp = filp;
  1417. riocm_debug(CHOP, "ch_%d created by %s(%d)",
  1418. ch_num, current->comm, task_pid_nr(current));
  1419. return put_user(ch_num, p);
  1420. }
  1421. /*
  1422. * cm_chan_close() - Close channel
  1423. * @filp: Pointer to file object
  1424. * @arg: Channel to close
  1425. */
  1426. static int cm_chan_close(struct file *filp, void __user *arg)
  1427. {
  1428. u16 __user *p = arg;
  1429. u16 ch_num;
  1430. struct rio_channel *ch;
  1431. if (get_user(ch_num, p))
  1432. return -EFAULT;
  1433. riocm_debug(CHOP, "ch_%d by %s(%d)",
  1434. ch_num, current->comm, task_pid_nr(current));
  1435. spin_lock_bh(&idr_lock);
  1436. ch = idr_find(&ch_idr, ch_num);
  1437. if (!ch) {
  1438. spin_unlock_bh(&idr_lock);
  1439. return 0;
  1440. }
  1441. if (ch->filp != filp) {
  1442. spin_unlock_bh(&idr_lock);
  1443. return -EINVAL;
  1444. }
  1445. idr_remove(&ch_idr, ch->id);
  1446. spin_unlock_bh(&idr_lock);
  1447. return riocm_ch_close(ch);
  1448. }
  1449. /*
  1450. * cm_chan_bind() - Bind channel
  1451. * @arg: Channel number
  1452. */
  1453. static int cm_chan_bind(void __user *arg)
  1454. {
  1455. struct rio_cm_channel chan;
  1456. if (copy_from_user(&chan, arg, sizeof(chan)))
  1457. return -EFAULT;
  1458. if (chan.mport_id >= RIO_MAX_MPORTS)
  1459. return -EINVAL;
  1460. return riocm_ch_bind(chan.id, chan.mport_id, NULL);
  1461. }
  1462. /*
  1463. * cm_chan_listen() - Listen on channel
  1464. * @arg: Channel number
  1465. */
  1466. static int cm_chan_listen(void __user *arg)
  1467. {
  1468. u16 __user *p = arg;
  1469. u16 ch_num;
  1470. if (get_user(ch_num, p))
  1471. return -EFAULT;
  1472. return riocm_ch_listen(ch_num);
  1473. }
  1474. /*
  1475. * cm_chan_accept() - Accept incoming connection
  1476. * @filp: Pointer to file object
  1477. * @arg: Channel number
  1478. */
  1479. static int cm_chan_accept(struct file *filp, void __user *arg)
  1480. {
  1481. struct rio_cm_accept param;
  1482. long accept_to;
  1483. struct rio_channel *ch;
  1484. if (copy_from_user(&param, arg, sizeof(param)))
  1485. return -EFAULT;
  1486. riocm_debug(CHOP, "on ch_%d by %s(%d)",
  1487. param.ch_num, current->comm, task_pid_nr(current));
  1488. accept_to = param.wait_to ?
  1489. msecs_to_jiffies(param.wait_to) : 0;
  1490. ch = riocm_ch_accept(param.ch_num, &param.ch_num, accept_to);
  1491. if (IS_ERR(ch))
  1492. return PTR_ERR(ch);
  1493. ch->filp = filp;
  1494. riocm_debug(CHOP, "new ch_%d for %s(%d)",
  1495. ch->id, current->comm, task_pid_nr(current));
  1496. if (copy_to_user(arg, &param, sizeof(param)))
  1497. return -EFAULT;
  1498. return 0;
  1499. }
  1500. /*
  1501. * cm_chan_connect() - Connect on channel
  1502. * @arg: Channel information
  1503. */
  1504. static int cm_chan_connect(void __user *arg)
  1505. {
  1506. struct rio_cm_channel chan;
  1507. struct cm_dev *cm;
  1508. struct cm_peer *peer;
  1509. int ret = -ENODEV;
  1510. if (copy_from_user(&chan, arg, sizeof(chan)))
  1511. return -EFAULT;
  1512. if (chan.mport_id >= RIO_MAX_MPORTS)
  1513. return -EINVAL;
  1514. down_read(&rdev_sem);
  1515. /* Find matching cm_dev object */
  1516. list_for_each_entry(cm, &cm_dev_list, list) {
  1517. if (cm->mport->id == chan.mport_id) {
  1518. ret = 0;
  1519. break;
  1520. }
  1521. }
  1522. if (ret)
  1523. goto err_out;
  1524. if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) {
  1525. ret = -EINVAL;
  1526. goto err_out;
  1527. }
  1528. /* Find corresponding RapidIO endpoint device object */
  1529. ret = -ENODEV;
  1530. list_for_each_entry(peer, &cm->peers, node) {
  1531. if (peer->rdev->destid == chan.remote_destid) {
  1532. ret = 0;
  1533. break;
  1534. }
  1535. }
  1536. if (ret)
  1537. goto err_out;
  1538. up_read(&rdev_sem);
  1539. return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel);
  1540. err_out:
  1541. up_read(&rdev_sem);
  1542. return ret;
  1543. }
  1544. /*
  1545. * cm_chan_msg_send() - Send a message through channel
  1546. * @arg: Outbound message information
  1547. */
  1548. static int cm_chan_msg_send(void __user *arg)
  1549. {
  1550. struct rio_cm_msg msg;
  1551. void *buf;
  1552. int ret;
  1553. if (copy_from_user(&msg, arg, sizeof(msg)))
  1554. return -EFAULT;
  1555. if (msg.size > RIO_MAX_MSG_SIZE)
  1556. return -EINVAL;
  1557. buf = memdup_user((void __user *)(uintptr_t)msg.msg, msg.size);
  1558. if (IS_ERR(buf))
  1559. return PTR_ERR(buf);
  1560. ret = riocm_ch_send(msg.ch_num, buf, msg.size);
  1561. kfree(buf);
  1562. return ret;
  1563. }
  1564. /*
  1565. * cm_chan_msg_rcv() - Receive a message through channel
  1566. * @arg: Inbound message information
  1567. */
  1568. static int cm_chan_msg_rcv(void __user *arg)
  1569. {
  1570. struct rio_cm_msg msg;
  1571. struct rio_channel *ch;
  1572. void *buf;
  1573. long rxto;
  1574. int ret = 0, msg_size;
  1575. if (copy_from_user(&msg, arg, sizeof(msg)))
  1576. return -EFAULT;
  1577. if (msg.ch_num == 0 || msg.size == 0)
  1578. return -EINVAL;
  1579. ch = riocm_get_channel(msg.ch_num);
  1580. if (!ch)
  1581. return -ENODEV;
  1582. rxto = msg.rxto ? msecs_to_jiffies(msg.rxto) : MAX_SCHEDULE_TIMEOUT;
  1583. ret = riocm_ch_receive(ch, &buf, rxto);
  1584. if (ret)
  1585. goto out;
  1586. msg_size = min(msg.size, (u16)(RIO_MAX_MSG_SIZE));
  1587. if (copy_to_user((void __user *)(uintptr_t)msg.msg, buf, msg_size))
  1588. ret = -EFAULT;
  1589. riocm_ch_free_rxbuf(ch, buf);
  1590. out:
  1591. riocm_put_channel(ch);
  1592. return ret;
  1593. }
  1594. /*
  1595. * riocm_cdev_ioctl() - IOCTL requests handler
  1596. */
  1597. static long
  1598. riocm_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1599. {
  1600. switch (cmd) {
  1601. case RIO_CM_EP_GET_LIST_SIZE:
  1602. return cm_ep_get_list_size((void __user *)arg);
  1603. case RIO_CM_EP_GET_LIST:
  1604. return cm_ep_get_list((void __user *)arg);
  1605. case RIO_CM_CHAN_CREATE:
  1606. return cm_chan_create(filp, (void __user *)arg);
  1607. case RIO_CM_CHAN_CLOSE:
  1608. return cm_chan_close(filp, (void __user *)arg);
  1609. case RIO_CM_CHAN_BIND:
  1610. return cm_chan_bind((void __user *)arg);
  1611. case RIO_CM_CHAN_LISTEN:
  1612. return cm_chan_listen((void __user *)arg);
  1613. case RIO_CM_CHAN_ACCEPT:
  1614. return cm_chan_accept(filp, (void __user *)arg);
  1615. case RIO_CM_CHAN_CONNECT:
  1616. return cm_chan_connect((void __user *)arg);
  1617. case RIO_CM_CHAN_SEND:
  1618. return cm_chan_msg_send((void __user *)arg);
  1619. case RIO_CM_CHAN_RECEIVE:
  1620. return cm_chan_msg_rcv((void __user *)arg);
  1621. case RIO_CM_MPORT_GET_LIST:
  1622. return cm_mport_get_list((void __user *)arg);
  1623. default:
  1624. break;
  1625. }
  1626. return -EINVAL;
  1627. }
  1628. static const struct file_operations riocm_cdev_fops = {
  1629. .owner = THIS_MODULE,
  1630. .open = riocm_cdev_open,
  1631. .release = riocm_cdev_release,
  1632. .unlocked_ioctl = riocm_cdev_ioctl,
  1633. };
  1634. /*
  1635. * riocm_add_dev - add new remote RapidIO device into channel management core
  1636. * @dev: device object associated with RapidIO device
  1637. * @sif: subsystem interface
  1638. *
  1639. * Adds the specified RapidIO device (if applicable) into peers list of
  1640. * the corresponding channel management device (cm_dev).
  1641. */
  1642. static int riocm_add_dev(struct device *dev, struct subsys_interface *sif)
  1643. {
  1644. struct cm_peer *peer;
  1645. struct rio_dev *rdev = to_rio_dev(dev);
  1646. struct cm_dev *cm;
  1647. /* Check if the remote device has capabilities required to support CM */
  1648. if (!dev_cm_capable(rdev))
  1649. return 0;
  1650. riocm_debug(RDEV, "(%s)", rio_name(rdev));
  1651. peer = kmalloc(sizeof(*peer), GFP_KERNEL);
  1652. if (!peer)
  1653. return -ENOMEM;
  1654. /* Find a corresponding cm_dev object */
  1655. down_write(&rdev_sem);
  1656. list_for_each_entry(cm, &cm_dev_list, list) {
  1657. if (cm->mport == rdev->net->hport)
  1658. goto found;
  1659. }
  1660. up_write(&rdev_sem);
  1661. kfree(peer);
  1662. return -ENODEV;
  1663. found:
  1664. peer->rdev = rdev;
  1665. list_add_tail(&peer->node, &cm->peers);
  1666. cm->npeers++;
  1667. up_write(&rdev_sem);
  1668. return 0;
  1669. }
  1670. /*
  1671. * riocm_remove_dev - remove remote RapidIO device from channel management core
  1672. * @dev: device object associated with RapidIO device
  1673. * @sif: subsystem interface
  1674. *
  1675. * Removes the specified RapidIO device (if applicable) from peers list of
  1676. * the corresponding channel management device (cm_dev).
  1677. */
  1678. static void riocm_remove_dev(struct device *dev, struct subsys_interface *sif)
  1679. {
  1680. struct rio_dev *rdev = to_rio_dev(dev);
  1681. struct cm_dev *cm;
  1682. struct cm_peer *peer;
  1683. struct rio_channel *ch, *_c;
  1684. unsigned int i;
  1685. bool found = false;
  1686. LIST_HEAD(list);
  1687. /* Check if the remote device has capabilities required to support CM */
  1688. if (!dev_cm_capable(rdev))
  1689. return;
  1690. riocm_debug(RDEV, "(%s)", rio_name(rdev));
  1691. /* Find matching cm_dev object */
  1692. down_write(&rdev_sem);
  1693. list_for_each_entry(cm, &cm_dev_list, list) {
  1694. if (cm->mport == rdev->net->hport) {
  1695. found = true;
  1696. break;
  1697. }
  1698. }
  1699. if (!found) {
  1700. up_write(&rdev_sem);
  1701. return;
  1702. }
  1703. /* Remove remote device from the list of peers */
  1704. found = false;
  1705. list_for_each_entry(peer, &cm->peers, node) {
  1706. if (peer->rdev == rdev) {
  1707. riocm_debug(RDEV, "removing peer %s", rio_name(rdev));
  1708. found = true;
  1709. list_del(&peer->node);
  1710. cm->npeers--;
  1711. kfree(peer);
  1712. break;
  1713. }
  1714. }
  1715. up_write(&rdev_sem);
  1716. if (!found)
  1717. return;
  1718. /*
  1719. * Release channels associated with this peer
  1720. */
  1721. spin_lock_bh(&idr_lock);
  1722. idr_for_each_entry(&ch_idr, ch, i) {
  1723. if (ch && ch->rdev == rdev) {
  1724. if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN)
  1725. riocm_exch(ch, RIO_CM_DISCONNECT);
  1726. idr_remove(&ch_idr, ch->id);
  1727. list_add(&ch->ch_node, &list);
  1728. }
  1729. }
  1730. spin_unlock_bh(&idr_lock);
  1731. if (!list_empty(&list)) {
  1732. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1733. list_del(&ch->ch_node);
  1734. riocm_ch_close(ch);
  1735. }
  1736. }
  1737. }
  1738. /*
  1739. * riocm_cdev_add() - Create rio_cm char device
  1740. * @devno: device number assigned to device (MAJ + MIN)
  1741. */
  1742. static int riocm_cdev_add(dev_t devno)
  1743. {
  1744. int ret;
  1745. cdev_init(&riocm_cdev.cdev, &riocm_cdev_fops);
  1746. riocm_cdev.cdev.owner = THIS_MODULE;
  1747. ret = cdev_add(&riocm_cdev.cdev, devno, 1);
  1748. if (ret < 0) {
  1749. riocm_error("Cannot register a device with error %d", ret);
  1750. return ret;
  1751. }
  1752. riocm_cdev.dev = device_create(dev_class, NULL, devno, NULL, DEV_NAME);
  1753. if (IS_ERR(riocm_cdev.dev)) {
  1754. cdev_del(&riocm_cdev.cdev);
  1755. return PTR_ERR(riocm_cdev.dev);
  1756. }
  1757. riocm_debug(MPORT, "Added %s cdev(%d:%d)",
  1758. DEV_NAME, MAJOR(devno), MINOR(devno));
  1759. return 0;
  1760. }
  1761. /*
  1762. * riocm_add_mport - add new local mport device into channel management core
  1763. * @dev: device object associated with mport
  1764. * @class_intf: class interface
  1765. *
  1766. * When a new mport device is added, CM immediately reserves inbound and
  1767. * outbound RapidIO mailboxes that will be used.
  1768. */
  1769. static int riocm_add_mport(struct device *dev,
  1770. struct class_interface *class_intf)
  1771. {
  1772. int rc;
  1773. int i;
  1774. struct cm_dev *cm;
  1775. struct rio_mport *mport = to_rio_mport(dev);
  1776. riocm_debug(MPORT, "add mport %s", mport->name);
  1777. cm = kzalloc(sizeof(*cm), GFP_KERNEL);
  1778. if (!cm)
  1779. return -ENOMEM;
  1780. cm->mport = mport;
  1781. rc = rio_request_outb_mbox(mport, cm, cmbox,
  1782. RIOCM_TX_RING_SIZE, riocm_outb_msg_event);
  1783. if (rc) {
  1784. riocm_error("failed to allocate OBMBOX_%d on %s",
  1785. cmbox, mport->name);
  1786. kfree(cm);
  1787. return -ENODEV;
  1788. }
  1789. rc = rio_request_inb_mbox(mport, cm, cmbox,
  1790. RIOCM_RX_RING_SIZE, riocm_inb_msg_event);
  1791. if (rc) {
  1792. riocm_error("failed to allocate IBMBOX_%d on %s",
  1793. cmbox, mport->name);
  1794. rio_release_outb_mbox(mport, cmbox);
  1795. kfree(cm);
  1796. return -ENODEV;
  1797. }
  1798. cm->rx_wq = create_workqueue(DRV_NAME "/rxq");
  1799. if (!cm->rx_wq) {
  1800. rio_release_inb_mbox(mport, cmbox);
  1801. rio_release_outb_mbox(mport, cmbox);
  1802. kfree(cm);
  1803. return -ENOMEM;
  1804. }
  1805. /*
  1806. * Allocate and register inbound messaging buffers to be ready
  1807. * to receive channel and system management requests
  1808. */
  1809. for (i = 0; i < RIOCM_RX_RING_SIZE; i++)
  1810. cm->rx_buf[i] = NULL;
  1811. cm->rx_slots = RIOCM_RX_RING_SIZE;
  1812. mutex_init(&cm->rx_lock);
  1813. riocm_rx_fill(cm, RIOCM_RX_RING_SIZE);
  1814. INIT_WORK(&cm->rx_work, rio_ibmsg_handler);
  1815. cm->tx_slot = 0;
  1816. cm->tx_cnt = 0;
  1817. cm->tx_ack_slot = 0;
  1818. spin_lock_init(&cm->tx_lock);
  1819. INIT_LIST_HEAD(&cm->peers);
  1820. cm->npeers = 0;
  1821. INIT_LIST_HEAD(&cm->tx_reqs);
  1822. down_write(&rdev_sem);
  1823. list_add_tail(&cm->list, &cm_dev_list);
  1824. up_write(&rdev_sem);
  1825. return 0;
  1826. }
  1827. /*
  1828. * riocm_remove_mport - remove local mport device from channel management core
  1829. * @dev: device object associated with mport
  1830. * @class_intf: class interface
  1831. *
  1832. * Removes a local mport device from the list of registered devices that provide
  1833. * channel management services. Returns an error if the specified mport is not
  1834. * registered with the CM core.
  1835. */
  1836. static void riocm_remove_mport(struct device *dev,
  1837. struct class_interface *class_intf)
  1838. {
  1839. struct rio_mport *mport = to_rio_mport(dev);
  1840. struct cm_dev *cm;
  1841. struct cm_peer *peer, *temp;
  1842. struct rio_channel *ch, *_c;
  1843. unsigned int i;
  1844. bool found = false;
  1845. LIST_HEAD(list);
  1846. riocm_debug(MPORT, "%s", mport->name);
  1847. /* Find a matching cm_dev object */
  1848. down_write(&rdev_sem);
  1849. list_for_each_entry(cm, &cm_dev_list, list) {
  1850. if (cm->mport == mport) {
  1851. list_del(&cm->list);
  1852. found = true;
  1853. break;
  1854. }
  1855. }
  1856. up_write(&rdev_sem);
  1857. if (!found)
  1858. return;
  1859. flush_workqueue(cm->rx_wq);
  1860. destroy_workqueue(cm->rx_wq);
  1861. /* Release channels bound to this mport */
  1862. spin_lock_bh(&idr_lock);
  1863. idr_for_each_entry(&ch_idr, ch, i) {
  1864. if (ch->cmdev == cm) {
  1865. riocm_debug(RDEV, "%s drop ch_%d",
  1866. mport->name, ch->id);
  1867. idr_remove(&ch_idr, ch->id);
  1868. list_add(&ch->ch_node, &list);
  1869. }
  1870. }
  1871. spin_unlock_bh(&idr_lock);
  1872. if (!list_empty(&list)) {
  1873. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1874. list_del(&ch->ch_node);
  1875. riocm_ch_close(ch);
  1876. }
  1877. }
  1878. rio_release_inb_mbox(mport, cmbox);
  1879. rio_release_outb_mbox(mport, cmbox);
  1880. /* Remove and free peer entries */
  1881. if (!list_empty(&cm->peers))
  1882. riocm_debug(RDEV, "ATTN: peer list not empty");
  1883. list_for_each_entry_safe(peer, temp, &cm->peers, node) {
  1884. riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev));
  1885. list_del(&peer->node);
  1886. kfree(peer);
  1887. }
  1888. riocm_rx_free(cm);
  1889. kfree(cm);
  1890. riocm_debug(MPORT, "%s done", mport->name);
  1891. }
  1892. static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
  1893. void *unused)
  1894. {
  1895. struct rio_channel *ch;
  1896. unsigned int i;
  1897. LIST_HEAD(list);
  1898. riocm_debug(EXIT, ".");
  1899. /*
  1900. * If there are any channels left in connected state send
  1901. * close notification to the connection partner.
  1902. * First build a list of channels that require a closing
  1903. * notification because function riocm_send_close() should
  1904. * be called outside of spinlock protected code.
  1905. */
  1906. spin_lock_bh(&idr_lock);
  1907. idr_for_each_entry(&ch_idr, ch, i) {
  1908. if (ch->state == RIO_CM_CONNECTED) {
  1909. riocm_debug(EXIT, "close ch %d", ch->id);
  1910. idr_remove(&ch_idr, ch->id);
  1911. list_add(&ch->ch_node, &list);
  1912. }
  1913. }
  1914. spin_unlock_bh(&idr_lock);
  1915. list_for_each_entry(ch, &list, ch_node)
  1916. riocm_send_close(ch);
  1917. return NOTIFY_DONE;
  1918. }
  1919. /*
  1920. * riocm_interface handles addition/removal of remote RapidIO devices
  1921. */
  1922. static struct subsys_interface riocm_interface = {
  1923. .name = "rio_cm",
  1924. .subsys = &rio_bus_type,
  1925. .add_dev = riocm_add_dev,
  1926. .remove_dev = riocm_remove_dev,
  1927. };
  1928. /*
  1929. * rio_mport_interface handles addition/removal local mport devices
  1930. */
  1931. static struct class_interface rio_mport_interface __refdata = {
  1932. .class = &rio_mport_class,
  1933. .add_dev = riocm_add_mport,
  1934. .remove_dev = riocm_remove_mport,
  1935. };
  1936. static struct notifier_block rio_cm_notifier = {
  1937. .notifier_call = rio_cm_shutdown,
  1938. };
  1939. static int __init riocm_init(void)
  1940. {
  1941. int ret;
  1942. /* Create device class needed by udev */
  1943. dev_class = class_create(THIS_MODULE, DRV_NAME);
  1944. if (IS_ERR(dev_class)) {
  1945. riocm_error("Cannot create " DRV_NAME " class");
  1946. return PTR_ERR(dev_class);
  1947. }
  1948. ret = alloc_chrdev_region(&dev_number, 0, 1, DRV_NAME);
  1949. if (ret) {
  1950. class_destroy(dev_class);
  1951. return ret;
  1952. }
  1953. dev_major = MAJOR(dev_number);
  1954. dev_minor_base = MINOR(dev_number);
  1955. riocm_debug(INIT, "Registered class with %d major", dev_major);
  1956. /*
  1957. * Register as rapidio_port class interface to get notifications about
  1958. * mport additions and removals.
  1959. */
  1960. ret = class_interface_register(&rio_mport_interface);
  1961. if (ret) {
  1962. riocm_error("class_interface_register error: %d", ret);
  1963. goto err_reg;
  1964. }
  1965. /*
  1966. * Register as RapidIO bus interface to get notifications about
  1967. * addition/removal of remote RapidIO devices.
  1968. */
  1969. ret = subsys_interface_register(&riocm_interface);
  1970. if (ret) {
  1971. riocm_error("subsys_interface_register error: %d", ret);
  1972. goto err_cl;
  1973. }
  1974. ret = register_reboot_notifier(&rio_cm_notifier);
  1975. if (ret) {
  1976. riocm_error("failed to register reboot notifier (err=%d)", ret);
  1977. goto err_sif;
  1978. }
  1979. ret = riocm_cdev_add(dev_number);
  1980. if (ret) {
  1981. unregister_reboot_notifier(&rio_cm_notifier);
  1982. ret = -ENODEV;
  1983. goto err_sif;
  1984. }
  1985. return 0;
  1986. err_sif:
  1987. subsys_interface_unregister(&riocm_interface);
  1988. err_cl:
  1989. class_interface_unregister(&rio_mport_interface);
  1990. err_reg:
  1991. unregister_chrdev_region(dev_number, 1);
  1992. class_destroy(dev_class);
  1993. return ret;
  1994. }
  1995. static void __exit riocm_exit(void)
  1996. {
  1997. riocm_debug(EXIT, "enter");
  1998. unregister_reboot_notifier(&rio_cm_notifier);
  1999. subsys_interface_unregister(&riocm_interface);
  2000. class_interface_unregister(&rio_mport_interface);
  2001. idr_destroy(&ch_idr);
  2002. device_unregister(riocm_cdev.dev);
  2003. cdev_del(&(riocm_cdev.cdev));
  2004. class_destroy(dev_class);
  2005. unregister_chrdev_region(dev_number, 1);
  2006. }
  2007. late_initcall(riocm_init);
  2008. module_exit(riocm_exit);