rio_cm.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377
  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. INIT_WORK(&cm->rx_work, rio_ibmsg_handler);
  1807. cm->tx_slot = 0;
  1808. cm->tx_cnt = 0;
  1809. cm->tx_ack_slot = 0;
  1810. spin_lock_init(&cm->tx_lock);
  1811. INIT_LIST_HEAD(&cm->peers);
  1812. cm->npeers = 0;
  1813. INIT_LIST_HEAD(&cm->tx_reqs);
  1814. down_write(&rdev_sem);
  1815. list_add_tail(&cm->list, &cm_dev_list);
  1816. up_write(&rdev_sem);
  1817. return 0;
  1818. }
  1819. /*
  1820. * riocm_remove_mport - remove local mport device from channel management core
  1821. * @dev: device object associated with mport
  1822. * @class_intf: class interface
  1823. *
  1824. * Removes a local mport device from the list of registered devices that provide
  1825. * channel management services. Returns an error if the specified mport is not
  1826. * registered with the CM core.
  1827. */
  1828. static void riocm_remove_mport(struct device *dev,
  1829. struct class_interface *class_intf)
  1830. {
  1831. struct rio_mport *mport = to_rio_mport(dev);
  1832. struct cm_dev *cm;
  1833. struct cm_peer *peer, *temp;
  1834. struct rio_channel *ch, *_c;
  1835. unsigned int i;
  1836. bool found = false;
  1837. LIST_HEAD(list);
  1838. riocm_debug(MPORT, "%s", mport->name);
  1839. /* Find a matching cm_dev object */
  1840. down_write(&rdev_sem);
  1841. list_for_each_entry(cm, &cm_dev_list, list) {
  1842. if (cm->mport == mport) {
  1843. list_del(&cm->list);
  1844. found = true;
  1845. break;
  1846. }
  1847. }
  1848. up_write(&rdev_sem);
  1849. if (!found)
  1850. return;
  1851. flush_workqueue(cm->rx_wq);
  1852. destroy_workqueue(cm->rx_wq);
  1853. /* Release channels bound to this mport */
  1854. spin_lock_bh(&idr_lock);
  1855. idr_for_each_entry(&ch_idr, ch, i) {
  1856. if (ch->cmdev == cm) {
  1857. riocm_debug(RDEV, "%s drop ch_%d",
  1858. mport->name, ch->id);
  1859. idr_remove(&ch_idr, ch->id);
  1860. list_add(&ch->ch_node, &list);
  1861. }
  1862. }
  1863. spin_unlock_bh(&idr_lock);
  1864. if (!list_empty(&list)) {
  1865. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1866. list_del(&ch->ch_node);
  1867. riocm_ch_close(ch);
  1868. }
  1869. }
  1870. rio_release_inb_mbox(mport, cmbox);
  1871. rio_release_outb_mbox(mport, cmbox);
  1872. /* Remove and free peer entries */
  1873. if (!list_empty(&cm->peers))
  1874. riocm_debug(RDEV, "ATTN: peer list not empty");
  1875. list_for_each_entry_safe(peer, temp, &cm->peers, node) {
  1876. riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev));
  1877. list_del(&peer->node);
  1878. kfree(peer);
  1879. }
  1880. riocm_rx_free(cm);
  1881. kfree(cm);
  1882. riocm_debug(MPORT, "%s done", mport->name);
  1883. }
  1884. static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
  1885. void *unused)
  1886. {
  1887. struct rio_channel *ch;
  1888. unsigned int i;
  1889. LIST_HEAD(list);
  1890. riocm_debug(EXIT, ".");
  1891. /*
  1892. * If there are any channels left in connected state send
  1893. * close notification to the connection partner.
  1894. * First build a list of channels that require a closing
  1895. * notification because function riocm_send_close() should
  1896. * be called outside of spinlock protected code.
  1897. */
  1898. spin_lock_bh(&idr_lock);
  1899. idr_for_each_entry(&ch_idr, ch, i) {
  1900. if (ch->state == RIO_CM_CONNECTED) {
  1901. riocm_debug(EXIT, "close ch %d", ch->id);
  1902. idr_remove(&ch_idr, ch->id);
  1903. list_add(&ch->ch_node, &list);
  1904. }
  1905. }
  1906. spin_unlock_bh(&idr_lock);
  1907. list_for_each_entry(ch, &list, ch_node)
  1908. riocm_send_close(ch);
  1909. return NOTIFY_DONE;
  1910. }
  1911. /*
  1912. * riocm_interface handles addition/removal of remote RapidIO devices
  1913. */
  1914. static struct subsys_interface riocm_interface = {
  1915. .name = "rio_cm",
  1916. .subsys = &rio_bus_type,
  1917. .add_dev = riocm_add_dev,
  1918. .remove_dev = riocm_remove_dev,
  1919. };
  1920. /*
  1921. * rio_mport_interface handles addition/removal local mport devices
  1922. */
  1923. static struct class_interface rio_mport_interface __refdata = {
  1924. .class = &rio_mport_class,
  1925. .add_dev = riocm_add_mport,
  1926. .remove_dev = riocm_remove_mport,
  1927. };
  1928. static struct notifier_block rio_cm_notifier = {
  1929. .notifier_call = rio_cm_shutdown,
  1930. };
  1931. static int __init riocm_init(void)
  1932. {
  1933. int ret;
  1934. /* Create device class needed by udev */
  1935. dev_class = class_create(THIS_MODULE, DRV_NAME);
  1936. if (IS_ERR(dev_class)) {
  1937. riocm_error("Cannot create " DRV_NAME " class");
  1938. return PTR_ERR(dev_class);
  1939. }
  1940. ret = alloc_chrdev_region(&dev_number, 0, 1, DRV_NAME);
  1941. if (ret) {
  1942. class_destroy(dev_class);
  1943. return ret;
  1944. }
  1945. dev_major = MAJOR(dev_number);
  1946. dev_minor_base = MINOR(dev_number);
  1947. riocm_debug(INIT, "Registered class with %d major", dev_major);
  1948. /*
  1949. * Register as rapidio_port class interface to get notifications about
  1950. * mport additions and removals.
  1951. */
  1952. ret = class_interface_register(&rio_mport_interface);
  1953. if (ret) {
  1954. riocm_error("class_interface_register error: %d", ret);
  1955. goto err_reg;
  1956. }
  1957. /*
  1958. * Register as RapidIO bus interface to get notifications about
  1959. * addition/removal of remote RapidIO devices.
  1960. */
  1961. ret = subsys_interface_register(&riocm_interface);
  1962. if (ret) {
  1963. riocm_error("subsys_interface_register error: %d", ret);
  1964. goto err_cl;
  1965. }
  1966. ret = register_reboot_notifier(&rio_cm_notifier);
  1967. if (ret) {
  1968. riocm_error("failed to register reboot notifier (err=%d)", ret);
  1969. goto err_sif;
  1970. }
  1971. ret = riocm_cdev_add(dev_number);
  1972. if (ret) {
  1973. unregister_reboot_notifier(&rio_cm_notifier);
  1974. ret = -ENODEV;
  1975. goto err_sif;
  1976. }
  1977. return 0;
  1978. err_sif:
  1979. subsys_interface_unregister(&riocm_interface);
  1980. err_cl:
  1981. class_interface_unregister(&rio_mport_interface);
  1982. err_reg:
  1983. unregister_chrdev_region(dev_number, 1);
  1984. class_destroy(dev_class);
  1985. return ret;
  1986. }
  1987. static void __exit riocm_exit(void)
  1988. {
  1989. riocm_debug(EXIT, "enter");
  1990. unregister_reboot_notifier(&rio_cm_notifier);
  1991. subsys_interface_unregister(&riocm_interface);
  1992. class_interface_unregister(&rio_mport_interface);
  1993. idr_destroy(&ch_idr);
  1994. device_unregister(riocm_cdev.dev);
  1995. cdev_del(&(riocm_cdev.cdev));
  1996. class_destroy(dev_class);
  1997. unregister_chrdev_region(dev_number, 1);
  1998. }
  1999. late_initcall(riocm_init);
  2000. module_exit(riocm_exit);